[Vuejs]-How to iterate all object in data section?

0👍

You need Object.keys:

for (el in Object.keys(this.main_level))

If you need both the key and value, you can use Object.entries:

for (el in Object.entries(this.main_level))
var data =  {
   main_level:
   {  
    user_input_1 : 
    {
     isFilled : false,
     val_1 : null,
     val_2 :null,
     val_3 : null,
    },
    user_input_2 : 
    {
     isFilled : false,
     val_1 : null,
     val_2 :null,
     val_3 : null,
    }
   }
}

console.log(Object.keys(data.main_level))

0👍

Your code is almost correct, just change the for-loop from in to of:

el in Object.values(this.main_level) to el of Object.values(this.main_level)

Here is a working example:

methods: {
  myclick: function () {
    for (obj of Object.values(this.main_level)) {
      console.log(obj)
    }
  }
}

https://jsfiddle.net/j0pd1q2z/

Leave a comment