Js processing methods that cannot define two-dimensional arrays themselves
This article mainly introduces how to deal with js processing methods that cannot define two-dimensional arrays. If you need them, please refer to them and hope to help you.
The Code is as follows:
Var a = new Array (1, 2), new Array ('B', 'C '));
Document. write (a [1] [1]);
To put it bluntly, we use the for loop to define a two-dimensional array!
?
<Script language = "javascript" type = "text/javascript">
Function Array_2 (nRow, nColumn ){
Var array1 = new Array (); // defines a one-dimensional Array
For (I = 0; I <nRow; I ++ ){
// Define each child element as an array
Array1 [I] = new Array ();
//----------------------------------------
For (n = 0; n <nColumn; n ++ ){
Array1 [I] [n] = ''; // at this time, aa [I] [n] can be considered as a list-level array.
}
//--------------------------------------
}
Return array1;
}
Var array_2 = Array_2 (3, 2 );
Array_2 [0] [1] = 1;
Array_2 [0] [2] = 2;
Array_2 [1] [1] = 3;
Array_2 [1] [2] = 4;
Document. write (array_2 [1] [2]);
</Script>
// The dotted line can also be implemented using the push () method of the js Array built-in object, because when arr1.push (arr2, the entire array arr2 is added to the arr1 array as an element. Therefore, the for loop in the dotted line can be replaced with the following statement: array1 [I]. push (new Array (nColumn ));
Today, we also found that the definition can also be made into a two-dimensional array;
The Code is as follows:
Var a = new Array (1, 2), new Array ('B', 'C '));
Document. write (a [1] [1]);
Ps: note the difference between push and concat!
The push method adds new elements in the order they appear. If one of the parameters is an array, the array will be added to the array as a single element. To merge two or more elements in an array, use the concat method.
The concat method returns an Array object that contains the connection between array1 and any other projects provided. Project to be added (item1... ItemN) is added to the array from left to right. If an item is an array, add its content to the end of array1. If the project is not an array, add it as a single array element to the end of the array.
Good !!!