[英]Pushing values into an object of array
So I have an array of objects called products:所以我有一个名为 products 的对象数组:
const products = [
{
name: 'Pants'
},
{
name: 'Shoes'
},
{
name: 't shirts'
}
]
And the list contains a lot more products with other values apart from name.并且该列表包含更多具有除名称之外的其他值的产品。 So suppose i forgot to give each product, (object) a coupon's array, it would take me quite a while to update each object manually.
所以假设我忘了给每个产品,(对象)一个优惠券的数组,手动更新每个 object 需要很长时间。
Is there a function that I can create which will push a coupons value into each object as an array?是否有我可以创建的 function 将优惠券值作为数组推送到每个 object 中? So the final array would look like:
所以最终的数组看起来像:
const updatedProducts = [
{
name: 'Pants',
coupons: []
},
{
name: 'Shoes',
coupons: []
},
{
name: 't shirts',
coupons: []
}
]
const updatedProducts = products.map(product => ({ ...product, coupons: [] }))
Yes you can add properties in javascript like this: products.forEach(product => product.coupons = []);
是的,您可以像这样在 javascript 中添加属性:
products.forEach(product => product.coupons = []);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.