1

I have a 3d array, the first level array can have one to many items (arrays). Each inner array has a fixed length, and its elements are also arrays of fixed length. In the example below length 3 and 3 respectively. I would like sum the respective inner arrays, [1+1+1, 2+2+2, 3+3+3]. The output should be a 2d array with a 3 x 3 shape.

let arr = [
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
];

//expected output = [[3,6,9],[12,15,18],[21,24,27]]  

I have tried many approaches the but the best I can get:

let arr = [
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
];
let data = [];

arr.forEach((e) => {
  data.push(e.reduce((r, a, i) => a.map((b, j) => r[j] + b)));
})

// returns [[12, 15, 18], [12, 15, 18], [12, 15, 18]]
console.log(data);

But this is the sum [1+4+7, 2+5+8, 3+6+9].

4 Answers 4

4

You could reduce the outer array, because this dimension is mapped over the values for the 2d arrays.

let array = [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]],
    sum = array.reduce((a, b) => b.map((x, i) => x.map((v, j) => a[i][j] + v)));

console.log(sum); // [[3, 6, 9], [12, 15, 18], [21, 24, 27]]
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

4 Comments

I implemented this code within a method of an object, and it does not work, it throws no errors but it returns an empty array, after the first iteration. I tested it as a stand alone operation with the same data and it works fine. I added an empty array as an "initial value" and with it, it gets through two iterations, stops and stills returns an empty array. I can't figure this out.
maybe your access to the array does not work. can you add your implementation?
I'll add it as new question.
here is the link to new question. stackoverflow.com/questions/62237704/…
1

You can use a simple for loop

let arr = [
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
];

let final = [[],[],[]]

for(let i=0; i<arr[0].length; i++){
  for(let j=0; j<arr[0][i].length; j++){
     for(let k=0; k<arr[0][j].length; k++){
      final[i][j] = (final[i][j]||0)+arr[k][i][j]
    }
  }
}

console.log(final)

Comments

1

First create empty output array with 0 values. Use nested forEach loops and fill the output values.

let arr = [
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
  ],
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
  ],
  [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
  ],
];

const output = Array.from({ length: arr[0].length }, () =>
  new Array(arr[0][0].length).fill(0)
);

arr.forEach((array) =>
  array.forEach((rows, row) =>
    rows.forEach((val, col) => (output[row][col] += val))
  )
);

console.log(output);

Comments

0

A solution where the length of the top level array can be different -- so that maybe it has 4 or 5 grids, and thus results in sums of 4 or 5 values:

let arr = [[[1, 2, 3],[4, 5, 6],[7, 8, 9]],[[1, 2, 3],[4, 5, 6],[7, 8, 9]],[[1, 2, 3], [4, 5, 6],[7, 8, 9]],];

let data = arr[0].map((row, i) =>
  row.map((_, j) => arr.reduce((sum, sub) => sum + sub[i][j]))
);

console.log(data);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.