获取每个数组中所有第一项的总和,每个数组中的所有第二项等.ES6

时间:2017-06-28 15:41:35

标签: javascript arrays reactjs ecmascript-6 array.prototype.map

我正在尝试将未确定数量的数组加在一起,但只能逐项添加或减少它们。

我无法弄清楚如何在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方法但是空了

0 个答案:

没有答案
相关问题