1
I’m trying to loop (for) an array object in Vue-Js, (javascript) whose size is 11 (lenght=11) as below. But when I do:
console.log(this.grouped) // Here it shows the object with length=11
for (var i=0;i<10; i++){
console.log(i+" Nome:"+this.agrupados.product_name )
}
loop does not work. It is as if lenght is null and not 11.
On inspection of Chrome presents this:
The code I have is:
created() {
this.getProducts() // Atualiza valores a receber
console.log(this.agrupados)
for (var i=0;i<10; i++){
console.log(i+" Nome:"+this.agrupados.product_name )
}
},
Can you show the code that this is inserted into? That is, it shows more code around your problem.
– Sergio
Flavius, he actually finds the data and so he does it. The problem is that since it is an array you need to pass the position of the array that contains the attribute. Pass on the console.log(this.agrupados[i]. product_name should solve your problem.
– Lucas Brogni
Tried to
this.agrupados[i].product_name
?– NoobSaibot
created() { this.getProducts() // Updates values to receive console.log(this.grouped) for (var i=0;i<10; i++){ console.log(i+" Name:"+this.grouped.product_name ) } },
– Flavio Cardoso
Flavio, what does the
this.getProducts()
and what givesconsole.log(JSON.stringify(this.agrupados))
?– Sergio
Search the recorded data in the firestore. console.log and group the data, totaling, as shown in the figure above, in the Chrome inspection
– Flavio Cardoso
use the code @Noobsaibot, I think it will work
– picossi
Flavio, so we can help you quickly, without having to guess, which
console.log(JSON.stringify(this.agrupados))
?– Sergio
gives the following: [ ]
– Flavio Cardoso
a pair of clasps
– Flavio Cardoso
Console.log (this.grouped) yields the data below:
– Flavio Cardoso
Grouped:[] List.See? 16cf:120 0 Name:Undefined List.See? 16cf:120 1 Name:Undefined List.See? 16cf:120 2 Name:Undefined List.See? 16cf:120 3 Name:Undefined List.See? 16cf:120 4 Name:Undefined List.See? 16cf:120 5 Name:Undefined List.See? 16cf:120 6 Name:Undefined List.See? 16cf:120 7 Name:Undefined List.See? 16cf:120 8 Name:Undefined List.See? 16cf:120 9 Name:Undefined
– Flavio Cardoso
The Result above when I do the code below:
– Flavio Cardoso
console.log("Grouped:"+JSON.stringify(this.grouped)) for (var i=0;i<10; i++){ console.log(i+" Name:"+this.grouped[i].product_name ) }
– Flavio Cardoso
The strange thing is that when I rewrite the html, it works. It shows the data right. Now when I try to loop the script, it gives: Undefined
– Flavio Cardoso
@Flaviocardoso gives himself
[]
That’s very important for the problem. You have an asynchronous logic there and you’ll need to usecomputed
probably. What makes thethis.getProducts()
?– Sergio
Take the data in the firestore and group, totalized and generating the grouped array
– Flavio Cardoso
I did the calculation on computed and gave the same message
– Flavio Cardoso
I suggest you put in the whole component so we can understand the logic that you have
– Sergio
Unfortunately I haven’t gotten the solution to my problem yet. I appreciate if anyone can help me
– Flavio Cardoso
Solved with the suggestion of @Noobsaibot. Grateful. Really needed to put the index.: this.agrupados[i]. product_name. Thank you very much
– Flavio Cardoso