KShewengger
KShewengger

Reputation: 8269

Sum of nested arrays according to their indexes

If I have these corresponding array with 2 nested arrays (this may have 2 or more) inside:

const nums = [
    [4, 23, 20, 23, 6, 8, 4, 0],      // Each array consists of 8 items
    [7, 5, 2, 2, 0, 0, 0, 0] 
];

How can I perform addition with them in accordance to their indexes ?

Expected Result:

// 11 is from 4 (array1 - index1) + 7 (array2 - index1)
// and so on.
[11, 28, 22, 25, 6, 8, 4, 0]

What I did was:

// This will work but it will only be applicable for 2 arrays as what if there will be 2 or more making it dynamic 

const total = Array.from({ length: 8 }, (_, i) => nums[0][i] + nums[1][i]);

Upvotes: 1

Views: 709

Answers (5)

Shidersz
Shidersz

Reputation: 17190

One possible solution is to Array.map() each element of the first inner array to the sum of elements in the same column. For get the summatory of elements in the same column we can use Array.reduce() inside the map():

const nums = [
  [4, 23, 20, 23, 6, 8, 4, 0],
  [7, 5, 2, 2, 0, 0, 0, 0],
  [1, 3, 4, 7, 1, 1, 1, 1],
];

let [first, ...rest] = nums;
let res = first.map((e, i) => rest.reduce((sum, x) => sum + x[i], e));

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Upvotes: 3

Jorge Ocampo
Jorge Ocampo

Reputation: 56

This support n nested arrays

const nums = [
  [4, 23, 20, 23, 6, 8, 4, 0],
  [7, 5, 2, 2, 0, 0, 0, 0],
  [2, 1, 2, 5, 7, 8, 9, 4]
];


const total = nums.reduce((a, b) => a.map((c, i) => c + b[i]));

console.log(total);

Upvotes: 3

BlueWater86
BlueWater86

Reputation: 1817

You can use reduce and an inner loop. Some of the things to be careful of are different array lengths & values that are not numbers.

const nums = [
    [4, 23, 20, 23, 6, 8, 4, 0],      // Each array consists of 8 items
    [7, 5, 2, 2, 0, 0, 0, 0] 
];
const otherNums = [
    [4, 23, 20, 23, 6, 8, 4, 0, 9, 55],      // Each array consists of 8 items
    [7, 5, 2, 2, 0, 0, 0, 0, "cat", null, 78],
    [7, 5, 2, 2, 0, 0, 0, 0, "dog", null, 78],
    [7, 5, 2, 2, 0, 0, 0, 0, "elephant", null, 78] 
];

const sumArraysByIndex = nums => nums.reduce((sums, array) => {
  for (const index in array) {
    if (sums[index] === undefined) sums[index] = 0
    if (isNaN(array[index])) return sums
    sums[index] += array[index]
  }
  return sums
}, [])

console.log(sumArraysByIndex(nums))
console.log(sumArraysByIndex(otherNums))

Upvotes: 1

random
random

Reputation: 7899

Get the minimum length of subarray and then create the array of that length, using index return the addition.

const nums = [
    [4, 23, 20, 23, 6, 8, 4, 0],
    [7, 5, 2, 2, 0, 0, 0, 0]
];

const [arr1, arr2] = nums;
const min = Math.min(nums[0].length, nums[1].length);

const output = Array.from({length: min}, (_, i) => arr1[i] + arr2[i]);

console.log(output);

Upvotes: 0

Maheer Ali
Maheer Ali

Reputation: 36594

You can use nested forEach() loop

const nums = [
    [4, 23, 20, 23, 6, 8, 4, 0],
    [7, 5, 2, 2, 0, 0, 0, 0] 
];

function sum(arr){
  let max = Math.max(...arr.map(x => x.length));
  let res = Array(max).fill(0)
  res.forEach((x,i) => {
    nums.forEach(a => {
      
      res[i] = res[i] +  (a[i] || 0)
    })
  })
  return res;
}

console.log(sum(nums))

Upvotes: 1

Related Questions