SoFunction
Updated on 2025-03-03

Detailed explanation of JavaScript implementing array dimensionality reduction

Dimension reduction of two-dimensional arrays

There are only two dimensions of a two-dimensional array, and dimensionality reduction is relatively simple, so there is no need to consider too complex algorithm logic. Let’s take a look at several methods for dimensionality reduction of two-dimensional arrays;

Traversal dimension reduction

var arr = [
  ['h', 'e', 'l', 'l', 'o'],
  ['m', 'y'],
  ['w', 'o', 'r', 'l', 'd'],
  ['!']
];
var result = [];
for (var r = 0; r < ; r++) {
  for (var c = 0; c < arr[r].length; c++) {
    (arr[r][c]);
  }
}
(result); //=>[ 'h', 'e', 'l', 'l', 'o', 'm', 'y', 'w', 'o', 'r', 'l', 'd', '!' ]

This method has a simple idea, using a double loop to traverse each element in a two-dimensional array and put it into a new array.

Using concat

Using the concat method, double loops can be simplified into single loops:

var arr = [
  ['h', 'e', 'l', 'l', 'o'],
  ['m', 'y'],
  ['w', 'o', 'r', 'l', 'd'],
  ['!']
];
var result = [];
for (var r = 0, result = []; r < ; r++) {
  result = (arr[r]);
}
(result); //=>[ 'h', 'e', 'l', 'l', 'o', 'm', 'y', 'w', 'o', 'r', 'l', 'd', '!' ]

Each element of arr is an array or parameter. As a parameter of the concat method, the parameters or each child element in the array will be inserted into the new array independently.

Use apply+concat

The apply method will call a function, the first parameter of the apply method will be used as this value of the called function, and the second parameter of the apply method (an array, or an object of the class array) will be used as the arguments value of the called object, that is, each element of the array will become the parameters of the called function in turn;

var arr = [
  ['h', 'e', 'l', 'l', 'o'],
  ['m', 'y'],
  ['w', 'o', 'r', 'l', 'd'],
  ['!']
];
var result = ([], arr);
(result); //=>[ 'h', 'e', 'l', 'l', 'o', 'm', 'y', 'w', 'o', 'r', 'l', 'd', '!' ]

Using the apply method, you can complete the dimensionality reduction of two-dimensional arrays with just one line of code.

Multidimensional array dimension reduction

Multidimensional arrays are not as simple as two-dimensional arrays. Because the depth of the array is not determined, it cannot be traversed to reduce the dimensions. They can only be achieved through recursion or stack methods.

recursion

 = function() {
  var result = []; //Define the array to save the results  (function(val, idx) { //Transfer the array    if ((val)) { //Distinguish whether it is a subarray      (); //It is a subarray and execute recursively    } else {
      (val); //If you are not a subarray, the value will be stored in the result array    }
  });
  return result; //Return result array}
var arr = [2, 3, [2, 2],
  [3, 'f', ['w', 3]], { "name": 'Tom' }
];
(()); //=&gt;[ 2, 3, 2, 2, 3, 'f', 'w', 3, { name: 'Tom' } ]

This is a recursive method to realize the dimensionality reduction of multi-dimensional arrays. In this case, I used a prototype chain to encapsulate the method into the Array prototype, and can be called directly in the array method.

Stack method

 = function() {
  var result = []; //Define the array to save the results  var stack = this; //Put the array into the stack  while ( !== 0) { //If the stack is not empty, then loop through    var val = (); //Take out the last value    if ((val)) { //Judge whether it is an array      stack = (val); //If it is an array, it will be spliced ​​into the stack    } else {
      (val); //If it is not an array, take it out and put it into the result array    }
  }
  return result;
}
var arr = [2, 3, [2, 2],
  [3, 'f', ['w', 3]], { "name": 'Tom' }
];
(()); //=&gt;[ 2, 3, 2, 2, 3, 'f', 'w', 3, { name: 'Tom' } ]

This is through the stack method, a stack is created, the contents of the array are stored, and then one by one is taken out. If the array is taken out, the array is broken and spliced ​​into the stack, and one is released, and this loop is looped.

The method of dimensionality reduction of multi-dimensional arrays can also reduce dimensionality by two-dimensional arrays, but it is a bit useless. It is better to use the right method to do the right thing!

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.