Javascript sum inner arrays in 3d nested arrays

blindChicken

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].

Nina Scholz

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; }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Sum arrays in javascript

分類Dev

Sum of nested arrays according to their indexes

分類Dev

Javascript - sum 2d arrays

分類Dev

How to write a nested array where the inner arrays are different lengths?

分類Dev

How to sum two arrays data in JavaScript

分類Dev

Efficiently computing sum of cross product for two 3D arrays in R

分類Dev

infinite inner arrays with push() method in javascript chrome console

分類Dev

Sum of 2 different 2d arrays

分類Dev

Nested structs of arrays in Go

分類Dev

Parsing Nested Arrays Data

分類Dev

Display nested arrays in vue

分類Dev

Javascript ES6 calculate the sum of an array of arrays of objects

分類Dev

Javascript loop question : nested arrays access json hierarchy

分類Dev

Multiplying 3 arrays to form a 3D array with entries multiplied

分類Dev

Need to form an array object with nested data out of 3 different arrays

分類Dev

Searching for value in nested php arrays

分類Dev

Finding local maxima in large 3D Numpy arrays

分類Dev

numpy 3D array vectorized access with arrays of indices

分類Dev

Ruby array of arrays find by inner array value

分類Dev

Julia truncate inner dimension of array of arrays

分類Dev

Confused about arrays on javascript

分類Dev

arr is not defined in Javascript Arrays

分類Dev

Assigning Javascript arrays to innerHTML

分類Dev

Beginner javascript arrays

分類Dev

Javascript: Split array of arrays into multiple arrays

分類Dev

Python: sum over arrays element by element

分類Dev

Looking for a faster way to sum arrays in C#

分類Dev

Sum column numbers elements in many arrays

分類Dev

Merging the elements of nested arrays into one big array

Related 関連記事

ホットタグ

アーカイブ