let list= [["现金",12,1,8],["算款",5,0,0],["回购",1,0,0],["到期",10,0,20],["存款",0,10,0],["其它",0,50,0]]//每个数组里面对应下标累积相加,最后希望得到下面结果,请问该用什么方法去实现呢{"0":"合计","1":28,"2":61,"3":28}
1 回答
叮当猫咪
TA贡献1776条经验 获得超12个赞
这个情景下用reduce比较符合语义
let list = [['现金', 12, 1, 8], ['算款', 5, 0, 0], ['回购', 1, 0, 0], ['到期', 10, 0, 20], ['存款', 0, 10, 0], ['其它', 0, 50, 0]];let result = list.reduce((res, arr) => {
arr.forEach((num, index) => { if (index) {
res[index] = (res[index] || 0) + num;
}
}); return res;
}, {'0': '合计'});console.log(result);添加回答
举报
0/150
提交
取消
