Apply the array dimensionality reduction method Magical -JavaScript

Be tolerant to diversity, tolerance is a virtue

1, the ordinary cycle conversion method

The multi-dimensional array (especially two-dimensional array) into a one-dimensional array is used in the logic of business development, in addition to using simple conversion cycle, we can also use the Javascript language features to achieve a more simple and elegant conversion. This article from the simple conversion cycle began, one by one introduced three common conversion method, and to a brief review of methods and Function.prototype.apply Array.prototype.concat method.
The following code will be the two-dimensional array dimensionality reduction to one-dimensional array, for example.

Copy the code
function reduceDimension(arr) {
    var reduced = [];
    for (var i = 0; i < arr.length; i++) {
        for (var j = 0; j < arr[i].length; j++) {
            reduced.push(arr[i][j]);
        }
    }
    return reduced;
}
Copy the code

This method is a simple idea, using a double loop through each element of a two-dimensional array and into the new array.

2、 利用concat转换
先来回顾一下MDN上对于该方法的介绍: 
"concat creates a new array consisting of the elements in the object on which it is called, followed in order by, for each argument, the elements of that argument (if the argument is an array) or the argument itself (if the argument is not an array)."
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

That is, if a process parameter concat element that is inserted directly into the new array; If the parameter is an array, each element of the array to be inserted into the new array; attribute to the code:

Copy the code
function reduceDimension(arr) {
    var reduced = [];
    for (var i = 0; i < arr.length; i++){
        reduced = reduced.concat(arr[i]);
    }
    return reduced;
}
Copy the code

arr of each element is an array, the method concat as a parameter, each sub-element in the array and will be inserted independently into the new array.
Use concat method, we will simplify the dual circulation to the singlet cycle.

3, apply and use concat conversion
in accordance with the convention, let's look at the MDN describes methods to apply:
"at The apply () Method, Calls A function with the this value and arguments The A GIVEN AS AN Provided Array."
HTTPS: // Developer. mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

I.e. apply method calls a function, the first parameter of the method will apply this value as the called function, the second parameter apply method (an array, an array of classes or objects) will be called a target value of arguments, that is to say each element of the array will be successively respective parameters of the called function; attribute to the code:

function reduceDimension(arr) {
    return Array.prototype.concat.apply([], arr);
}

arr apply method as the second parameter, and is itself an array, each array element (or array, i.e., the second dimension of the two-dimensional array) is sequentially passed as a parameter to the concat, the effect is equivalent to [] .concat ([1,2], [3,4], [5,6]).

Reprinted from: https://www.cnblogs.com/exhuasted/p/7833263.html

Guess you like

Origin www.cnblogs.com/planetwithpig/p/11735193.html