search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

2 answers
Q&A Push data and keys into array in Vue.js
Push data and keys into array in Vue.js
P粉564192131 2024-02-25 14:22:51 Views 570

0

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

Your Answer
submit

2 answers
0

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

2024-02-26 10:21:41

submit

0

Try changing the pushData method to have the data parameter

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

Calling method

this.pushData({name: "john", age: 25});
2024-02-26 12:46:26

submit

Popular tool

vc9-vc14 (32+64 bit) runtime library collection (link below)

vc9-vc14 (32+64 bit) runtime library collection (link below)

Download the collection of runtime libraries required for phpStudy installation

VC9 32-bit

VC9 32-bit

VC9 32-bit phpstudy integrated installation environment runtime library

PHP programmer toolbox full version

PHP programmer toolbox full version

Programmer Toolbox v1.0 PHP Integrated Environment

VC11 32-bit

VC11 32-bit

VC11 32-bit phpstudy integrated installation environment runtime library

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use