Sum two arrays in single iteration
JavascriptArraysLoopsJavascript Problem Overview
I want to sum each value of an array of numbers with its corresponding value in a different array of numbers, and I want to do this without looping through each individual value.
So:
var array1 = [1,2,3,4];
var array2 = [5,6,7,8];
var sum = [6,8,10,12];
I'd love to do it in one fell swoop, instead of doing this:
for(var i = 0; i < array1.length; i++){
sum.push(array1[i] + array2[i]);
}
Can anyone think of a way? I'm pretty stumped.
Javascript Solutions
Solution 1 - Javascript
I know this is an old question but I was just discussing this with someone and we came up with another solution. You still need a loop but you can accomplish this with the Array.prototype.map().
var array1 = [1,2,3,4];
var array2 = [5,6,7,8];
var sum = array1.map(function (num, idx) {
return num + array2[idx];
}); // [6,8,10,12]
Solution 2 - Javascript
Here is a generic solution for N arrays of possibly different lengths.
It uses Array.prototype.reduce(), Array.prototype.map(), Math.max() and Array.from():
function sumArrays(...arrays) {
const n = arrays.reduce((max, xs) => Math.max(max, xs.length), 0);
const result = Array.from({ length: n });
return result.map((_, i) => arrays.map(xs => xs[i] || 0).reduce((sum, x) => sum + x, 0));
}
console.log(...sumArrays([0, 1, 2], [1, 2, 3, 4], [1, 2])); // 2 5 5 4
Solution 3 - Javascript
var arr = [1,2,3,4];
var arr2 = [1,1,1,2];
var squares = arr.map((a, i) => a + arr2[i]);
console.log(squares);
Solution 4 - Javascript
Below example will work even with length variation and few more use cases. check out. you can do prototyping as well if you needed.
function sumArray(a, b) {
var c = [];
for (var i = 0; i < Math.max(a.length, b.length); i++) {
c.push((a[i] || 0) + (b[i] || 0));
}
return c;
}
// First Use Case.
var a = [1, 2, 3, 4];
var b = [1, 2, 3, 4];
console.log( sumArray(a, b) );
// Second Use Case with different Length.
var a = [1, 2, 3, 4];
var b = [1, 2, 3, 4, 5];
console.log( sumArray(a, b) );
// Third Use Case with undefined values and invalid length.
var a = [1, 2, 3, 4];
var b = [];
b[1] = 2;
b[3] = 4;
b[9] = 9;
console.log( sumArray(a, b) );
Solution 5 - Javascript
Just merge Popovich and twalters's answer.
Array.prototype.SumArray = function (arr) {
var sum = this.map(function (num, idx) {
return num + arr[idx];
});
return sum;
}
var array1 = [1,2,3,4];
var array2 = [5,6,7,8];
var sum = array1.SumArray(array2);
console.log(sum); // [6,8,10,12]
Solution 6 - Javascript
Another way to do it could be like that
var array1 = [1,2,3,4];
var array2 = [5,6,7,8];
var sum = [...array1].map((e,i)=> e+array2[i]); //[6,8,10,12]
In this case [...array1]
is the same to [1,2,3,4]
Solution 7 - Javascript
You can't avoid the loop, but you can do this once and add a function to all Array objects using Array.prototype
.
Here's and example:
// Add a SumArray method to all arrays by expanding the Array prototype(do this once in a general place)
Array.prototype.SumArray = function (arr) {
var sum = [];
if (arr != null && this.length == arr.length) {
for (var i = 0; i < arr.length; i++) {
sum.push(this[i] + arr[i]);
}
}
return sum;
}
// here's your code
var array1 = [1, 2, 3, 4];
var array2 = [5, 6, 7, 8];
var sum = array1.SumArray(array2);
console.log(sum); // [6,8,10,12]
Here's your Fiddle.
Solution 8 - Javascript
In ES6+, you can use arrow function to make the code clear:
var x = [1,2,3];
var y = [2,3,4];
var sum = x.map( (val, i) => val + y[i] );
console.log(sum); //Array [3, 5, 7]
var z = [3,4,5];
var add3 = x.map( (val, i) => val + y[i] + z[i] );
console.log(add3); //Array [6, 9, 12]
Solution 9 - Javascript
You can use the _.unzipWith
method from the Lodash library.
var array1 = [1, 2, 3, 4];
var array2 = [5, 6, 7, 8];
var array = [array1, array2];
console.log(_.unzipWith(array, _.add));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Solution 10 - Javascript
For all of the beginners coming across this question who may not be at the level to use map / reduce or ternary operators..
let sum = 0;
let nums = []
for (let i = 0; i < array1.length; i++){
sum = array1[i] + array2[i];
nums.push(sum)
}
return nums
Solution 11 - Javascript
You can do it using some functional style:
const a = [1,2,3]
const b = [4,5,6]
const f= a.concat(b).map((v,i,arr)=>{
if ( i<arr.length) return v+arr[i+arr.length/2]
}).filter(n=>!isNaN(n))
console.log(f)
Solution 12 - Javascript
This example will work with different length variation:
let getSum = (arr1, arr2) => {
let main = arr1.length >= arr2.length ? arr1 : arr2;
let sec = arr1.length < arr2.length ? arr1 : arr2;
return main.map((elem, i) => sec[i] ? elem + sec[i] : elem)
}
Solution 13 - Javascript
Generic solution for N arrays of possibly different lengths using functional programming in one line ;)
const sumArrays = as => as.filter(a => a.length).length ? [as.filter(a => a.length).reduce((r, a) => r + a.shift(), 0), ...sumArrays(as)] : []console.log(sumArrays([[1, 2, 3], [100], [11, 22, 33, 44], []]))
Solution 14 - Javascript
All the above mentioned answer is correct,
Using reduce. I just want to add my answer might be simple and useful.
var array1 = [1,2,3,4];
var array2 = [5,6,7,8];
const reducer = (accumulator, currentValue, index, array) => {
let val = currentValue + array2[index];
accumulator.push(val);
return accumulator;
}
console.log(array1.reduce(reducer, []));
Thanks..
Solution 15 - Javascript
let array1=[];
let array2=[];
let array3=[];
let sum=0;
// Take elements from the user for 1st Array
for(let i=0; i<3; i++){
array1[i]=Number(prompt(i));
}
console.log(array1);
// Take elements from the user for 2nd Array
for(let j=0; j<3; j++){
array2[j]=Number(prompt(j));
}
console.log(array2);
// add sum of two arrays in 3rd Array
for(k=0; k<3; k++){
sum=(array1[k]+array2[k]);
array3.push(sum);
}
console.log(array3);