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
2 answers
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 }}
Try changing the pushData method to have the data parameter
pushData(data) {
this.list.push(data);
}
Calling method
this.pushData({name: "john", age: 25});
Hot tools Tags
Hot Questions
Popular tool
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 phpstudy integrated installation environment runtime library
PHP programmer toolbox full version
Programmer Toolbox v1.0 PHP Integrated Environment
VC11 32-bit
VC11 32-bit phpstudy integrated installation environment runtime library
SublimeText3 Chinese version
Chinese version, very easy to use
Hot Topics
20417
7
13577
4






