我有一个以下数组:[ { "model": "A", "count": "10", "date": "02-12-2004" }, { "model": "B", "count": "20", "date": "02-01-2004" }, { "model": "C", "count": "30", "date": "02-02-2004" }]我需要一个新的数组集如下[ { "model": "A", "count": "10", "date": "02-12-2004" }, { "model": "B", "count": "30", "date": "02-01-2004" }, { "model": "C", "count": "60", "date": "02-02-2004" }]这意味着所有以前的集合计数都应该添加到下一个集合中。
1 回答
饮歌长啸
TA贡献1951条经验 获得超3个赞
试试这个。
arr=[{"model":"A", "count":"10","date":"02-12-2004"},{"model":"B", "count":"20","date":"02-01-2004"},{"model":"C", "count":"30","date":"02-02-2004"}]
count=0;
let arr2 = arr.filter(obj=>{
count+=parseInt(obj.count);
obj.count=count;
})
console.log(arr)
如果您不想修改现有数组。 Object.assign使用如下方法
arr=[{"model":"A", "count":"10","date":"02-12-2004"},{"model":"B", "count":"20","date":"02-01-2004"},{"model":"C", "count":"30","date":"02-02-2004"}]
count=0;
let arr2 = arr.map(obj=>{
count += parseInt(obj.count);
return Object.assign({}, obj, {
count:count
});
});
console.log(arr2)
添加回答
举报
0/150
提交
取消