我有一个对象数组。所有对象都具有以下键和值:
const arr=[ { name: 'beer', amount: 50, price: 50 }]
function changeValue(){
// here I would like to receive back an array with the same object keys and values except amount.
// arr.amount should be 0
}
<button onclick='changeValue()'> Reset Amount </button>
我如何找回具有相同对象的相同精确数组,除了每个对象的数量应为0?我认为可以使用传播算子来完成。
您可以使用rest参数。
const arr=[ { name: 'beer', amount: 100, price: 50 }, { name: 'test', amount: 50, price: 10 }];
const res = arr.map(({amount, ...rest})=>({...rest, amount: 0}));
console.log(res);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句