Attempting to make a for in a vue-js object

0

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 )
  }

The loop does not work. It is as if lenght were null and not 11.

In the chrome inspection it says this:

ThecodeIhaveis:

created(){this.getProducts()//Atualizavaloresareceberconsole.log(this.agrupados)for(vari=0;i<10;i++){console.log(i+" Nome:"+this.agrupados.product_name ) 
    } 
},
    
asked by anonymous 14.06.2018 / 13:10

2 answers

0

To loop an array in JavaScript there are basically three forms:

1 . Javascript classic (using the for with a counter):

const arrayExemplo = ['A', 'B', 'D'];

for (let i = 0; i < arrayExemplo.length; i++) {
    console.log('${i} ${arrayExemplo[i]}');
}

2 . Javascritp modern (uses the structure for ... of ):

const arrayExemplo = ['A', 'B', 'D'];

for (const value of arrayExemplo) {
    console.log('${value}')
}

3 . Using the forEach ( which is part of the Array object).

const arrayExemplo = ['A', 'B', 'D'];

arrayExemplo.forEach(function(item, index) {
    console.log('${index} ${item}');
})
    
15.06.2018 / 02:40
-2

const user = {}; user.name = "Eduardo"; user.idade = 21;

for(let item in user){

 // item =>  Nome da propriedade
 // user[item] => Valor da propriedade

 console.log( item + ' => ' + user[item] )

}
    
26.11.2018 / 16:14