Return to incorrect IF condition

Asked

Viewed 119 times

0

I make a request in the api to get some data, and then I foreach and give the result for a variable this.DadosQueTaNaTela soon after I make another request in another api to take the data and do a foreach and the result I give to this.OlhaDadosDaApiJson and I need to create foreach inside foreach pq it does not give the expected result.

The data that comes in the two variables are strings. Ex.: 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 equal, IE, should only show on the console the message "Same data", but shows on the console everything that is on Else also.

Does anyone know why this problem?

Note: I will put the full code that comes before the if to get a better sense

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 THE CONSOLE BEFORE IF

The value 36 is what you see in the two API’s

inserir a descrição da imagem aqui

IF VALUE inserir a descrição da imagem aqui

  • tries with 3 equal ===

  • I’ve tried it and it still won’t, it always shows off

  • Convert to number before the if

  • Continues the same way, showing the Else with the same data

  • The numbers are not equal then. Use the 3 equal and convert the two to numbers. There is no way to go wrong.

  • @Eduardovargas wants to compare the value simply, if with == did not work, with === is that it doesn’t work for sure :)

  • It doesn’t work with either of you

  • It in addition to show the same data which are: 24, 25 it also shows this data in LSE beyond that I am not equal

  • Gives a console.log(this.DadosQueTaNaTela.FI, this.OlhaDadosDaApiJson.FI) and put here the result

  • The foreach loop must be entering both conditions, as it must be making more than one comparison. Let’s say it will go 2 laps: in the first 15 == 15 it will show dados iguais, and in the second round, say 15 == 24, it will show Dados diferentes.

  • @sam and how do I stop it? pq I need the foreach to be able to access the data that is in the array individually

  • Before if (this.DadosQueTaNaTela.FI == this.OlhaDadosDaApiJson.FI) { Place the console.log(this.DadosQueTaNaTela.FI) console.log(this.OlhaDadosApiJson.FI) And show the result that appeared on the console.

  • Ready. I put the photo showing the result in the question @Herbertjunior

  • @So Mary as I understand it, he passes in if and in Else? Or just in Else?

  • He passes both, but then he should only show the right results. In the if is to show the different ones, which is only the 04, and in the Else show the same as the 34. Only it only results the last time, as shown in the image and not already at first

  • It would be interesting to differentiate the nomenclature from the variables, because they are repeating within the same block and can bring inconsistency . Ex: Sponse, element.

  • @Maria this.Looked at Apisdajson and this.Dataquetanatela, both are arrays? And you want to compare what you have on one and not on the other?

  • @Duiliobenjoino did it and it turned out the same.

  • @Herbertjunior exactly! Then I make a map on the two to make this comparison

  • @Maria, look if it helps the answer .. from what I understood it would be this.

Show 15 more comments

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.

  • but where is the other array? o this.DadosQueTaNaTela

  • @Maria there, I altered, and the second, will be the sought and the first will be the one that will seek ...

  • @maria worked?

  • apparently yes...but I put an It should appear those who are not equal, right? but ends up appearing the same also

  • http://pt-br.tinypic.com/r/psu34/9 this is the result of Else, where only those that are not equal should appear (the same is the number 24). and that http://pt-br.tinypic.com/r/24mylhf/9 is the result of if (where equals should appear)

  • @Maria Here in my work, is blocked these sites of images hehe, so I could see in your code what got confused was the loop, and not the IF and ELSE ... so what I did worked, I did not do much, just remade the loop. And if it worked, accept it as a response :)

  • But my previous code does the same as your...

  • @Maria think the problem is in the fact that you do not close the loop so it gave the equal result, as it is inside a foreach, it will pass on the condition of being equal and soon after it will be different... then after finding the equal, would have to have a break; in order to end the tie ... then it really goes through the 2 conditions, but not at the same time ... Then after going through the condition of being equal he will go through the different... in his code who is the variable "grav"?

  • grav is a variable of type any, which is just above the constructor, I just created it to put the result of the foreach in it

  • this.grav.foreach(variable => { this.Lookedapijson = variable if (this.DadosQueTaNaTela.FI == this.LlhaDadosDaApiJson.FI) { console.log('Same data') console.log(this.DadosQueTaNaTela.FI) }Else{ console.log('Different data') console.log(this.DadosQueTaNaTela.FI) } }) In this snippet you are making a loop on this variable ... @Maria, I think the error may be in this logic ... to put a result in a vector, you need to use this.vetor.push(value) ...

  • So instead of the foreach it should be a push?

  • If you want to insert the die into an array, yes ... you should use the push

  • What I need is to go through the array to get the data separately

  • From what I understand you want what you have in array 1 that you don’t have in array 2 correct? And this result would have to put in another array ... right?

  • that’s exactly what I want

  • I changed the method there to add what you found equal in a part vector, just put the type of the = vector to the elements, because it is as any[]

  • he’s saving only the same, but what I need is for him to save the different ones, you understand?

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.