1 回答
TA贡献1868条经验 获得超4个赞
您可以使用reduce、forEach和Object.keys方法执行以下操作,
const value = {
"2020-10-15":[
{"name":"France","value":20},
{"name":"United States","value":20},
{"name":"Italy","value":5},
],
"2020-10-16":[
{"name":"Germany","value":10},
{"name":"United States","value":5},
{"name":"France","value":10}
],
"2020-10-17":[],
"2020-10-18":[
{"name":"UK","value":10},
{"name":"Italy","value":10},
]
}
let res = Object.keys(value).reduce((prev, curr) => {
value[curr].forEach(item => {
const idx = prev.findIndex(pItem => pItem.name === item.name);
if(idx > -1) {
const newObj = {...prev[idx], value: prev[idx].value + item.value};
prev[idx] = newObj;
return ;
}
prev.push(item);
})
return prev;
}, []);
console.log(res);
添加回答
举报