我有以下格式的数据,
const data = [{
key:'key1',
rows:[
["A","2018","B","1"],
["A","2018","C","5"],
["A","2018","B","4"],
["A","2018","C","2"],
]
},
{
key:'key1',
rows:[
["A","2018","B","10"],
["A","2018","C","50"],
["A","2018","B","40"],
["A","2018","C","20"],
]
}]
我想首先rows根据第三个值减少每个数组中的最后一个值并求和。输出应该是这样的
const data = [{
key:'key1',
rows:[
["A","2018","B","5"],
["A","2018","C","7"]
]
},
{
key:'key1',
rows:[
["A","2018","B","50"],
["A","2018","C","70"]
]
}]
在此之后,我想合并这两个对象并将rows. 最终输出应该是这样的
const data = [{
key:'key1',
rows:[
["A","2018","B","55"],
["A","2018","C","77"]
]
}]
我试过这个,但没有得到想要的输出,
data.map(data => ({
...data,
rows: data.rows.reduce((array, value) => {
if(array.length > 0){
let found = false;
array.map(m => {
if(m[2] === value[2]){
found = true;
return [...m, m[3] = parseInt(m[3]) + parseInt(value[3])]
}
})
if(!found){
array.push(value)
}
return array
}else{
array.push(value)
return array
}
}, [])
}))
凤凰求蛊
相关分类