Home > Web Front-end > Front-end Q&A > JavaScript three-dimensional array sum

JavaScript three-dimensional array sum

王林
Release: 2023-05-09 12:16:07
Original
894 people have browsed it

JavaScript is a widely used programming language that is widely used in web development. In this language, using arrays to handle large amounts of data has become a common technique. Sometimes we need to use three-dimensional arrays to process data, such as processing three-dimensional spatial coordinates. This article will introduce how to use JavaScript to perform sum operations on three-dimensional arrays, and help readers learn more about the use of arrays in JavaScript.

First let’s take a look at what a three-dimensional array is. A three-dimensional array is formed by superimposing multiple two-dimensional arrays. Each array contains a certain number of elements, and these elements are arranged in three dimensions. In JavaScript, we can define a three-dimensional array in the following way:

let array3D = [
  [
    [1, 2],
    [3, 4]
  ],
  [
    [5, 6],
    [7, 8]
  ]
];
Copy after login

We can perform a sum operation on all elements in this three-dimensional array.

First, we need to convert this three-dimensional array into a one-dimensional array so that the elements can be traversed. We can use nested loops to implement multi-level traversal and add all elements in a three-dimensional array to a one-dimensional array.

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

let array1D = [];

for (let i = 0; i < array3D.length; i++) {
  for (let j = 0; j < array3D[i].length; j++) {
    for (let k = 0; k < array3D[i][j].length; k++) {
      array1D.push(array3D[i][j][k]);
    }
  }
}

console.log(array1D); // [1, 2, 3, 4, 5, 6, 7, 8]
Copy after login

Now that we have converted the three-dimensional array into a one-dimensional array, we can sum the elements in it. We can use the reduce() function to add the elements in the array to get the sum of all elements in the three-dimensional array.

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

let array1D = [];

for (let i = 0; i < array3D.length; i++) {
  for (let j = 0; j < array3D[i].length; j++) {
    for (let k = 0; k < array3D[i][j].length; k++) {
      array1D.push(array3D[i][j][k]);
    }
  }
}

let sum = array1D.reduce((a, b) => a + b);

console.log(sum); // 36
Copy after login

In the code, we use the reduce() function to add all elements in array1D. The reduce() function accepts two parameters, one is a callback function, which is used to operate on the elements in the array; the other is the initial value, which can specify the first element in the array or 0. In this example, all elements in the array are added together and the initial value is set to 0.

The above is how to sum three-dimensional arrays using JavaScript. By converting a three-dimensional array into a one-dimensional array and summing it using the reduce() function, you can get the sum of all elements in the three-dimensional array. This method can not only process three-dimensional arrays, but also arrays of any dimension, which provides great convenience for processing large-scale data. I hope this article can help readers better master array operations in JavaScript language.

The above is the detailed content of JavaScript three-dimensional array sum. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template