这组数据中 怎么把name的值相同的number相加?原数据希望统计的结果var res = [{ "name": "apple", "number": "19",}, { "name": "orange", "number": "25",}, { "name": "banana", "number": "1",}];
1 回答
慕莱坞森
TA贡献1810条经验 获得超4个赞
注意number 是个字符串
let tmp = {}
res.forEach(v => tmp[v.name] = (tmp[v.name] || 0) + Number(v.number))
res = Object.keys(tmp).map(name => ({ name, number: tmp[name]}))
console.log(res)
添加回答
举报
0/150
提交
取消