cart(){
let itemList= this.orderService.getCart()
let obj = {
quantity : 1
}
itemList.push(obj);
this.product = itemList
console.log("get products",this.product);
}
这是我的component.ts文件,并且我正在获取对象数组,我想在对象数组中推送值为“ 0”的对象“数量”。
我有这个回应
this.orderService.getCart() = [{id: 48, title: "Mango Juices", price: "30", category: "juices"}]
现在,当我要推动时,我得到了这个结果。
0:{id: 48, title: "Mango Juices", price: "30", category: "juices"}
1:{quantity: 1}
最佳答案
如果我理解你的问题,你需要类似的东西
ngOnInit() {
let itemList = [{ id: 48, title: "Mango Juices", price: "30", category: "juices" }]
itemList.forEach((key) => {
key["quantity"] = 0;
})
this.product = itemList
console.log("get products", this.product); //[{id: 48, title: "Mango Juices", price: "30", category: "juices", quantity: 0}]
}
关于angular - 如何在 Angular 6的对象数组中推送对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51493226/