There is:

[1, 2, 3, 0] [1, 2, 3,] [1, 2] 

We need the result in the form of a single array, where the indices will be the sum of the columns, i.e.

 [3, 6, 6, 0] 

UPDATED: Answer:

 var array = [ [1, 2, 3, 0], [1, 2, 3], [1, 2] ], result = array.reduce(function (r, a) { a.forEach(function (b, i) { r[i] = (r[i] || 0) + b; }); return r; }, []); 

ps @ Oleg Degtev kicked in the right direction =)

  • My answer is absolutely not true, you need to use another solution, for example @Qwertiy - user200141
  • All the rules, the main thing is to set the direction - Fikret

5 answers 5

UPD: The answer is wrong, counted the numbers in the wrong direction.

 console.log( [ [1, 2, 3, 0].reduce( function(sum, i) {return sum + i} ), [1, 2, 3,].reduce( function(sum, i) {return sum + i} ), [1, 2].reduce( function(sum, i) {return sum + i} ) ] ); 

.reduce() traverses each element of array i and stores the intermediate result in sum .

UPD2: Since the daw is just opposite my answer, I’ll add another possible implementation that counts the amount in the right "direction" based on other answers

 console.log( [ [1, 2, 3, 0], [1, 2, 3], [1, 2] ].reduce(function (sum, arr) { arr.map(function(val, i) { sum[i] = val + (sum[i] || 0); }); return sum; }); ); 
  • it is strange why the answer is marked as correct if the result is not as expected in the question - Grundy
  • one
    Does it not bother you that this code returns [6, 6, 3] instead of the required [3, 6, 6, 0]? :) - Yaant
  • one
    The author is strange, he asked for one thing, but he accepted the answer, which does something completely different. - dzhioev
  • Нужен результат в виде одного массива, где индексы будут суммой столбцов . Answered this question, and then some kind of "magic" - user200141
  • one
    @Olegg Degtev, you actually summed up the lines - Grundy

For example, something like this:

 "use strict" let arr = [[1, 2, 3, 0], [1, 2, 3], [1, 2]]; let result = []; for (let i = 0; i < arr.length; i++) { let subarr = arr[i]; for (let j = 0; j < subarr.length; j++) { if (result[j] == undefined) result[j] = 0; result[j] += subarr[j]; } } console.log(result); // [3, 6, 6, 0] 

    Another option to reduce , based on the @Yaant response

     var arr = [[1, 2, 3, 0], [1, 2, 3], [1, 2]]; var result = arr.reduce(function(acc,cur){ return cur.reduce(function(innerAcc, innerCur,index){ innerAcc[index]=(innerAcc[index]||0)+innerCur; return innerAcc; },acc); },[]); document.write(JSON.stringify(result)); 

       [ [1, 2, 3, 0], [1, 2, 3], [1, 2] ].reduce(function (res, cur) { for (var q=0; q<cur.length; ++q) { if (!res[q]) res[q] = 0; res[q] += cur[q]; } return res; }, []) 
      • beat a little ahead :-) - Grundy

      It is possible without these your reduce() , in a simple way:

       function columnSum(arr) { var i, j, out = arr[0].slice(); for (i = 1; i < arr.length; i++) { for (j = 0; j < arr[i].length; j++) { out[j] = (j < out.length ? out[j] : 0) + arr[i][j]; } } return out; } document.body.innerText = columnSum([ [1, 2, 3, 0], [1, 2, 3], [1, 2] ]).join(', '); 

      The option (probably?) Is faster than that of @Yaant in that the first line is immediately copied to the output, rather than one element at a time.

      • Yeah, this is just the Yaant version :-) the only thing is if the ternary operator is replaced. - Grundy
      • a, i have an array less! :) - Sergiks
      • Aha, this is if you can change the content :-) - Grundy
      • You are right, to change the input is not gut. Updated the answer. My option (probably?) Is faster than that of @Yaant in that the first line is immediately copied to the output, rather than one element at a time. - Sergiks
      • You can check on jsperf, but I'm too lazy :) - Grundy