Note: This answer does NOT solve the issue in a single iteration.
Base Case Solution
You can split this issue into two separate issues.
First we need to combine the elements of elements of the different arrays that are present on the same index. This is commonly called "zipping arrays". You'll find a zip()
methods in a lot of libraries, but can easily create your own.
const array1 = [1,2,3,4];
const array2 = [5,6,7,8];
zip(array1, array2) //=> [ [1,5], [2,6], [3,7], [4,7] ]
If you aren't using a library that has this included, you must define your own. A simple variant looks like this:
function zip(...arrays) {
const length = Math.max(...arrays.map(array => array.length));
return Array.from({ length }, (_, i) => arrays.map(array => array[i]));
}
Now that we have combined the two arrays, we need to sum each of the nested arrays. For this we must first know how to sum values in a normal array.
Just like zipping arrays, summing them is such a common action that most libraries provide a helper. Check any libraries you're using before defining your own sum()
.
A normal array of numbers can produce a sum by using reduce()
.
numbers.reduce((sum, number) => sum + number, 0);
It's probably a good idea to store this in a new function definition. sum(numbers)
is far more descriptive than the numbers.reduce(...)
line.
function sum(numbers) {
return numbers.reduce((sum, number) => sum + number, 0);
}
We now have all the building blocks to produce the desired result.
const result = zip(array1, array2).map(numbers => sum(numbers));
//=> [6, 8, 10, 12]
// simplified
const result = zip(array1, array2).map(sum);
//=> [6, 8, 10, 12]
The above combines array1
and array2
like shown in 1. Then we map()
each of the resulting pairs to their sum.
const array1 = [1,2,3,4];
const array2 = [5,6,7,8];
const result = zip(array1, array2).map(sum);
console.log(result);
// helpers
function zip(...arrays) {
const length = Math.max(...arrays.map(array => array.length));
return Array.from({ length }, (_, i) => arrays.map(array => array[i]));
}
function sum(numbers) {
return numbers.reduce((sum, number) => sum + number, 0);
}
More Arrays
This solution allows you to easily change the number of arrays.
const result = zip(array1, array2, array3, array4).map(sum);
Non-Matching Array Lengths
Do note that it's expected that all the arrays do have the same length. If the length doesn't match the sum will result in NaN
, since number + undefined = NaN
zip([1,2,3], [4,5], [6])
//=> [ [1,4,6], [2,5,undefined], [3,undefined,undefined] ]
To solve this you could add another helper to remove "empty" values. For this we can introduce the compact()
helper, which removes null
and undefined
values from the array.
function compact(array) {
return array.filter(item => item != null); // removes both `null` and `undefined`
}
Here is an example usage:
zip([1,2,3], [4,5], [6]) // [ [1,4,6], [2,5, undefined], [3,undefined,undefined] ]
.map(compact) // [ [1,4,6], [2,5], [3] ]
.map(sum) // [ 11, 7, 3 ]
const array1 = [1,2,3];
const array2 = [4,5];
const array3 = [6];
const result = zip(array1, array2, array3).map(compact).map(sum);
console.log(result);
// helpers
function zip(...arrays) {
const length = Math.max(...arrays.map(array => array.length));
return Array.from({ length }, (_, i) => arrays.map(array => array[i]));
}
function sum(numbers) {
return numbers.reduce((sum, number) => sum + number, 0);
}
function compact(array) {
return array.filter(item => item != null);
}