angular - 如何以角度 6 将对象推入对象数组中
问题描述
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}]
}
推荐阅读
- discord.js - 狙击命令没有显示最后删除的消息
- reactjs - 如何将 Redux-Store 发送到多个组件?
- c++ - 以二进制形式读取文件,将其压缩并以二进制形式写回
- python - 将 df 中的列值与具有特定格式的另一个 df 的列值匹配?
- python-3.x - 将 pdf 文件页面转换为图像 - Wand
- css - 如何更改 ASP.NET Core Web App 导航栏背景颜色
- drag - 使用 Pymunk 提升、拖动或浮力
- microsoft-edge - 如何从命令行或注册表设置获取 microsoft edge chromium 版本
- python - 我无法使用 Python selenium 自动登录网站
- scala - 在 Scala Spark Dataframe 中展平嵌套的 json