Push data and keys into array in Vue.js
P粉564192131
P粉564192131 2024-02-25 14:22:51
0
2
379

I have an array in data():

data()  {
  return {
    list: [],
 }
},

methods: {
  pushData() {
     this.list.push({name:'yorn', age: 20});
  }
}

Now I want to push to the 'list' array in the following format, the key is info:

list [
     info [
     {
       name:yorn,
       age: 20
     }
  ]
 ]

I'm new to vuejs and javascript, so I need everyone's help. Please give me your opinion. Thanks

P粉564192131
P粉564192131

reply all(2)
P粉990008428

Try changing the pushData method to have the data parameter

pushData(data) {
 this.list.push(data);
}

Calling method

this.pushData({name: "john", age: 25});
P粉004287665

The above expected result is not a valid JSON. It should look like below:

list: [{
    info: [{
        name: yorn,
        age: 20
    }]
}]

Working Demonstration:

new Vue({
  el: '#app',
  data: {
    list: []
  },
  mounted() {
    this.pushData();
  },
  methods: {
    pushData() {
      this.list.push({info : [{name:'yorn', age: 20}] });
      // Or you can also use below one.
      // this.list[0].info.push({name:'yorn', age: 20});
    }
  }
})

{{ item.name }}

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template