l know my question is similar for too many questions. l have checked all answers and all answers are not give me what l want exactly.
l have this array:
[
{
"size": true,
"status": true,
"desc": "صحن مخلمة",
"name": "مخلمة",
"id": "QmzeJgg2F2"
}
]
l want to push new array object by index array above using this code:
items_local: any[] = [];
add_addons(index,data) {
this.items_local[index].addons.push(data.addons);
console.log(this.items_local);
}
expected output:
[
{
"addons": [ // new array pushing
{
"price": 10
},
{
"price": 10
}
],
"size": true,
"status": true,
"desc": "صحن مخلمة",
"name": "مخلمة",
"id": "QmzeJgg2F2"
}
]
Error:
ERROR TypeError: Cannot read property 'push' of undefined
your original object doesn't have a addons
key, hence you can't call the push property on it, first create that key and assign to empty array
add_addons(index,data) {
this.items_local[index].addons = []
this.items_local[index].addons.push(data.addons);
console.log(this.items_local);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.