Incorrect IF condition return

0

I make a request in the api to get some data, and then I do a forEach and give the result to a variable this.DadosQueTaNaTela soon after I make another request in another api to get the data and do a forEach and the result I give this.OlhaDadosDaApiJson being that I need to create forEach inside of forEach because it does not give the expected result.

The data that comes in both variables are strings. Eg: this.DadosQueTaNaTela = 15 and this.OlhaDadosDaApiJson = 15

With this I make an if condition:

        this.grav.forEach(variable => {
            this.OlhaDadosDaApiJson = variable
            if (this.DadosQueTaNaTela.FI == this.OlhaDadosDaApiJson.FI) {
                console.log('Dados iguais')
                console.log(this.DadosQueTaNaTela.FI)
            }else{
              console.log('Dados diferentes')
              console.log(this.DadosQueTaNaTela.FI)
            }
        })

The data I am comparing are the same, ie I should just show the console the message "Equal data", but it shows in the console everything that is in the other too.

Does anyone know why this problem?

Note: I'm going to put the complete code that comes before the if to get a better idea

this.MetaService.FiliaisMetaDiarias().subscribe(
  data => {
    const response = (data as any)
    this.objeto_retorno = JSON.parse(response._body);

    this.fil.length = 0
    this.filMetade.length = 0
    this.filNaoAtingida.length = 0


    this.objeto_retorno.forEach(element => {

      this.tots = element.TOTAL
      element.TOTAL = (element.TOTAL * 100).toFixed(3) + '%'
      element.TOTAL = element.TOTAL.toString().replace(".", ",")

      if (this.tots >= this.MetaAtingida) {

        this.fil.push({
          FI: element.FILIAL,
          porc: element.TOTAL
        });

        this.mainColor = 'MetaAtingida'

        this.fil.forEach(element => {
          this.DadosQueTaNaTela = element
        })

        this.MetaService.CheckOrderGet().subscribe(
          data => {
            const response = (data as any)
            this.obj = JSON.parse(response._body)

            this.obj.forEach(element => {
              this.grav = element
            })


          }, error => {
            console.log('error')
            console.log(error)
          }

        )

RESULT THAT APPEARS ON CONSOLE BEFORE IF

The value 36 is what is in the two API's

VALUEWITHINTHEIF

    
asked by anonymous 04.09.2018 / 17:50

1 answer

0
let array:any[] = [];

let exist:boolean = false;

this.DadosQueTaNaTela.forEach(apielement => {

  var api = apielement;
  existe = false;
  this.OlhaDadosDaApiJson.forEach(dadoselement => {

      var dados = dadoselement;

      if (!exite){

        if (dados == api){
            existe = true;
            array.push(dados);
        }

      }

  }
}

Now this method only finds if it has some equal value in the second vector ... if you want to save the value or something would have to implement another logic.

    
06.09.2018 / 18:18