0

I have (5) arrays. I'm trying to combine the (5) arrays into one array and then sum all the values together. I know with (2) arrays you can use the concat method but what about when you have (5) arrays?

const numArr = [132, 233, 5002, 3232, 3220];
const numArr2 = [123, 212, 535, 20, 2492];
const numArr3 = [90, 21, 32, 200, 71];
const numArr4 = [90, 16, 2, 200110, 12];
const numArr5 = [90, 21, 76, 240, 132];
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Does this answer your question? [How to calculate the sum of multiple arrays?](https://stackoverflow.com/questions/54712774/how-to-calculate-the-sum-of-multiple-arrays) – Jax-p Aug 05 '21 at 23:27
  • Not really. The link to the question you provided doesn't include spread operator in the solutions. – WeeklyButterfly34 Aug 06 '21 at 16:33

4 Answers4

3

You can use spread syntax to concatenate the arrays;

const numArr = [132, 233, 5002, 3232, 3220];
const numArr2 = [123, 212, 535, 20, 2492];
const numArr3 = [90, 21, 32, 200, 71];
const numArr4 = [90, 16, 2, 200110, 12];
const numArr5 = [90, 21, 76, 240, 132];

const concatenated = [...numArr, ...numArr2, ...numArr3, ...numArr4, ...numArr5];
console.log(concatenated);

To sum the values, you can use Array.reduce():

const numArr = [132, 233, 5002, 3232, 3220];
const numArr2 = [123, 212, 535, 20, 2492];
const numArr3 = [90, 21, 32, 200, 71];
const numArr4 = [90, 16, 2, 200110, 12];
const numArr5 = [90, 21, 76, 240, 132];

const concatenated = [...numArr, ...numArr2, ...numArr3, ...numArr4, ...numArr5];
const sum = concatenated.reduce((a, b) => a + b)
console.log(concatenated);
console.log(sum);
Spectric
  • 30,714
  • 6
  • 20
  • 43
1

I know with (2) arrays you can use the concat method but what about when you have (5) arrays?

You can add another 3 arrays to concat().

const numArr = [132, 233, 5002, 3232, 3220];
const numArr2 = [123, 212, 535, 20, 2492];
const numArr3 = [90, 21, 32, 200, 71];
const numArr4 = [90, 16, 2, 200110, 12];
const numArr5 = [90, 21, 76, 240, 132];

console.log(
  numArr.concat(numArr2, numArr3, numArr4, numArr5)
);

The concat() method is used to merge two or more arrays

Source: Array.prototype.concat() | MDN

Sum of array is already well described in another SO Answers like How to find the sum of an array of numbers.

Jax-p
  • 7,225
  • 4
  • 28
  • 58
1

When you have more than (2) arrays that you wish to combine you can use the spread operator. This will allow you to take the array values from the (5) arrays and push it into a new array.

Once you have all the values you can use the reduce method to sum the numbers.

const numArr = [132, 233, 5002, 3232, 3220];
const numArr2 = [123, 212, 535, 20, 2492];
const numArr3 = [90, 21, 32, 20000, 1];
const numArr4 = [90, 16, 2, 200110, 12];
const numArr5 = [90, 21, 76, 240, 132];

let mainArr = [];

arr = [...numArr, ...numArr2, ...numArr3, ...numArr4, ...numArr5];

mainArr.push(arr);

const addArrValues = (acc, value) => acc + value;
let finalValue = arr.reduce(addArrValues);
console.log('Sum is:', finalValue);
  • Here is documentation in case you want to learn about spread and reduce: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce – LearningCoder Aug 05 '21 at 23:29
0

You can use spread operator to merge the arrays and then apply a reduce to sum all the elements:

const numArr = [132, 233, 5002, 3232, 3220];
const numArr2 = [123, 212, 535, 20, 2492];
const numArr3 = [90, 21, 32, 200, 71];
const numArr4 = [90, 16, 2, 200110, 12];
const numArr5 = [90, 21, 76, 240, 132];

const sum = [ ...numArr, ...numArr2, ...numArr3, ...numArr4, ...numArr5 ]
    .reduce((a, x) => a + x, 0)

But possibly the better idea (in terms of performance due to allocation of temporary arrays) would be to sum all the arrays first and then sum the sums:

const sum = [ numArr, numArr2, numArr3, numArr4, numArr5 ]
    .map(x => x.reduce((a, x) => a + x, 0))
    .reduce((a, x) => a + x, 0)