我正在尝试将未确定数量的数组加在一起,但只能逐项添加或减少它们。
我无法弄清楚如何在ES6 javascript中完成这项工作。
const arrayList = [
[1, 2, 3],
[1, 2, 3],
[2, 2, 2]
];
const totalRow = arrayList.map((array) => {
const total = [];
array.map((number, i) => {
total[i] = (total[i] == undefined ? 0 : total[i]) + number;
});
return total;
});
//Output is just 3 more arrays with no change, as it seems
//the total variable resets to an empty array with each iteration.
//what i want is this output:
[4, 6, 8]
我做错了什么?我也试过了reduce方法但是空了