Home > Web Front-end > JS Tutorial > Interesting JavaScript question: Multidimensional array initialization

Interesting JavaScript question: Multidimensional array initialization

黄舟
Release: 2017-01-22 15:02:22
Original
2312 people have browsed it

Sometimes, we need to create a multi-dimensional array and initialize it to the default value we want.

Normally, we will first create a one-dimensional array, then reference the two-dimensional array within this one-dimensional array... until the N-dimensional array, and then fill its contents with the default value .

In this process, we need to write a lot of loops, which is inevitably a bit cumbersome. So, why not think of a way to write a tool method for multi-dimensional array initialization so that we can call it conveniently?

I guess you must want a method like this:

dim( d1 [,d2 [,d3 [... ]]], value )
Copy after login

Look at its parameter list, d1, d2, d3 represent the number of elements referenced by each dimension array, and value represents the initial value.

This value may be a function. If this is the case, we refer to the return value of the function.

Let's take a look at a few examples:

dim( 3,3,"x" ) // => [['x','x','x'],['x','x','x'],['x','x','x']]
Copy after login

Here the one-bit array refers to 3 two-dimensional arrays, and each two-dimensional array refers to 3 initialization values ​​'x'

dim( 2,2,2,0 ) // => [[[0,0],[0,0]],[[0,0],[0,0]]]
Copy after login

The rules here are the same as above

dim( 3, true ) // => [true,true,true]
Copy after login

The one-dimensional array here refers to 3 boolean values ​​true

var xxx = function(){ return "xX" }
dim( 2,5,xxx ) // => [['xX','xX','xX','xX','xX'],['xX','xX','xX','xX','xX']]
Copy after login

The initialization value here is a function, so we fill in The result it returns

This question requires recursion because it involves an N-dimensional array.

It is a process of constructing an array and traversing it in depth.

In my method, a parameter deep is used to record the current depth.

For example, dim(2,2,2,0), if the current deep is 0, then it corresponds to the first parameter 2, indicating that 2 arrays are to be created, that is, the value of deep and the parameter list The index of is consistent and represents how many arrays or values ​​to construct.

If deep reaches the deepest level, it means that initialization assignment can be performed, and the for loop fills the current array.

function dim(){  
    var len = arguments.length;  
    var args = Array.prototype.slice.call(arguments,0,len-1);  
    var content = arguments[len-1];  
    var result = [];  
      
    var traverse = function foo(from,deep){  
        var arg = args[deep];  
        if(deep < args.length - 1){  
            for(var i=0;i<arg;i++){  
                var array = [];  
                from.push(array);  
                foo(array,deep+1);  
            }  
        }  
        else{  
            for(var i=0;i<arg;i++){  
                if(typeof content === "function"){  
                    from.push(content());  
                }  
                else{  
                    from.push(content);  
                }  
            }  
        }  
    };  
    traverse(result,0);  
    return result;  
}
Copy after login

The above is an interesting JavaScript question: the content of multi-dimensional array initialization. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related labels:
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