3

I have multiple arrays in javascript and I want to do sum of those arrays and final array.

EX: Array1 = [1,9,10,11], Array2 = [5,8,7,2], Total = [6,17,17,13].
5
  • I don't really understand, what your problem is, you want to find the best solution from a performance perspective? Commented Jul 30, 2012 at 6:12
  • What is the "final array"? Do you want 1+9+10+11 + 5+8..., or do you want [1+5+6, 9+8+17, ...] Commented Jul 30, 2012 at 6:12
  • I tried creating one function.. MyArray.Sum() which is returning sum of all array elements.. but I want one final array which consists of sum of all passed multiple array elements shown in above example. Commented Jul 30, 2012 at 6:14
  • Array1 = [1,9,10,11], Array2 = [5,8,7,2], Result = [6,17,17,13]. Result is sum of array1 and array2 elements. Commented Jul 30, 2012 at 6:17
  • 1
    “Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it” (stackoverflow.com/help/on-topic), voting to close. Commented Aug 2, 2014 at 17:13

6 Answers 6

6

Pure function with multiple arrays:

function sum(arrays) {
  return arrays.reduce((acc, array) => acc.map((sum, i) => sum + array[i]), new Array(arrays[0].length).fill(0));
}

const arrays = [
  [4, 6, 3, 2],
  [1, 4, 7, 9],
  [4, 6, 3, 2],
  [1, 4, 7, 9]
];

const result = sum(arrays);
console.log(result);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer! Your one-liner is very good! I went into the Edit mode to play with it in the preview without saving and I got to understand it better, this was very useful
4
var Array1 = [1,9,10,11];
var Array2 = [5,8,7,2]; 
var Total = [];

for( var i = 0; i < Array1.length; i++)
{
    Total.push(Array1[i]+Array2[i]);
}

BTW, starting variable names in capital letters feels awkward.

Comments

1
var Array1 = [1,9,10,11];
var Array2 = [5,8,7,2];
var Total = new Array();
for(var i= 0;i<Math.min(Array1.length,Array2.length);i++){
  Total.push(Array1[i]+Array2[i]);
}
alert(Total);

Comments

1
function aSum(/*arrays list*/){
  var total=[],undefined;
  for(var i=0,l0=arguments.length;i<l0;i++)
    for(var j=0,arg=arguments[i],l1=arg.length;j<l1;j++)
      total[j]=(total[j]==undefined?0:total[j])+arg[j];
  return total;
}


var Array1 = [1,9,10,11], Array2 = [5,8,7,2], Array3 = [1,2,3,4,8];

console.log(aSum(Array1, Array2, Array3)); // [7, 19, 20, 17, 8]

Comments

1

Arrow function in ES6

var Array1 = [1,9,10,11];
var Array2 = [5,8,7,2]; 
var sum = Array1.map((val, idx) => val + Array2[idx]);

console.log(sum);

Comments

0

const a1 = [1, 9, 10, 11];
const a2 = [5, 8, 7, 2];

const a3 = Array.from(a1, (v, i) => v + a2[i]);

console.log(a3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.