0

I am trying to add together an undetermined amount of arrays, but only add or reduce them item by item.

I cannot figure out how to make this work in 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]

What am I doing wrong? I tried reduce method as well but came up empty

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
leemac33
  • 5
  • 4

0 Answers0