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
tries with 3 equal ===
– Eduardo Vargas
I’ve tried it and it still won’t, it always shows off
– Maria
Convert to number before the if
– Eduardo Vargas
Continues the same way, showing the Else with the same data
– Maria
The numbers are not equal then. Use the 3 equal and convert the two to numbers. There is no way to go wrong.
– Eduardo Vargas
@Eduardovargas wants to compare the value simply, if with
==
did not work, with===
is that it doesn’t work for sure :)– Ricardo Pontual
It doesn’t work with either of you
– Maria
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
– Maria
Gives a
console.log(this.DadosQueTaNaTela.FI, this.OlhaDadosDaApiJson.FI)
and put here the result– Sorack
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 showDados diferentes
.– Sam
@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
– Maria
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.
– Herbert Junior
Ready. I put the photo showing the result in the question @Herbertjunior
– Maria
@So Mary as I understand it, he passes in if and in Else? Or just in Else?
– Herbert Junior
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
– Maria
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.
– Duilio Benjoino
@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?
– Herbert Junior
@Duiliobenjoino did it and it turned out the same.
– Maria
@Herbertjunior exactly! Then I make a map on the two to make this comparison
– Maria
@Maria, look if it helps the answer .. from what I understood it would be this.
– Herbert Junior