Home >Web Front-end >JS Tutorial >Detailed explanation of how js handles two-dimensional arrays that cannot be defined by themselves_javascript skills

Detailed explanation of how js handles two-dimensional arrays that cannot be defined by themselves_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:57:371098browse

Copy code The code is as follows:

var a= new Array(new Array(1,2 ),new Array('b','c'));
document.write(a[1][1]);
To put it bluntly, it is to use a for loop to define a two-dimensional array!
?



//The dotted line part can also be implemented using the push() method of the js Array built-in object, because when arr1.push(arr2), the entire array arr2 will be added to the arr1 array as an element, so the dotted line The for loop in can be completely replaced with the following statement:
array1[i].push(new Array(nColumn));


I also discovered today that it can also be defined like this or made into a two-dimensional array;

Copy the code


ps: Pay attention to the difference between push and concat!
The

push
method will add new elements in the order they appear.
If one of the arguments is an array, then the array will be added to the array as a single element. If you want to combine elements from two or more arrays, use the concat method.
The

concat method returns an Array object containing the concatenation of array1 and any other items provided. The items to be added (item1...itemN) will be added to the array in order from left to right. If an item is an array, add its content to the end of array1. If the item is not an array, it is added to the end of the array as a single array element.

Very good! ! !

Statement:
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