This article mainly summarizes three methods for defining and initializing two-dimensional arrays of js. If you need them, you can refer to them and hope to help you. Method 1: define and initialize them directly, you can use
Var _ TheArray = [["0-1", "0-2"], ["1-1", "1-2"], ["2-1 ", "2-2"]
Method 2: Two-dimensional array with unknown length
Var tArray = new Array (); // declare a dimension first for (var k = 0; k
Input the required value to the defined array.
TArray [6] [1] = 5; // You can pass the value of 5 to the array to overwrite the initialization null.
Method 3: before that, the above two methods have problems. method 2, each definition is initialized. Although it can be modified dynamically later, it is still not feasible.
So I tried a method to dynamically pass values to the array.
Ps: array interesting phenomena encountered in practice
We thought that two-dimensional arrays could directly pass in values like the following.
For (var a = 0;
The result is that the value of the next array is received in tArray [a], and the content of matArray [a] is ignored. If you change the position, matArray [a] is behind, the value of addArray [a] is passed in.
Thinking: simple example:
The Code is as follows:
Var a = [1, 2];
Var B = [];
B [0] = a; // pass array a as an element of array B to array B
Alert (B [0] [1]); // 2
The above is the simplest two-dimensional array,
The above example is written in another way:
The Code is as follows:
Var B = [];
B [0] = [1, 2]; // pass the array [1, 2] into array B as an element of array B
Alert (B [0] [1]); // 2
We can see that the above B [0] = [1, 2] is usable.
The Code is as follows:
For (var a = 0;
TArray [a] = [matArray [a], addArray [a]; Modify () in the preceding example to [] to form a two-dimensional array.
};
Conclusion: method 3:
The Code is as follows:
For (var a = 0;
TArray [a] = [aArray [a], bArray [a], cArray [a]; you can also add dArray [a], eArray [a]
};
This situation applies to the situation where several arrays are known and combined into a two-dimensional array.
Create multi-dimensional arrays in JS
《script》 var allarray=new Array(); var res=""; function loaddata() { for(var i=0;i<3;i++) { var starth=i*200; var strarw=i*200; var endh=(i+1)*200; var endw=(i+1)*200; allarray[i]=new Array(); allarray[i][0]=new Array(); allarray[i][1]=new Array(); allarray[i][0][0]=starth; allarray[i][0][1]=strarw; allarray[i][1][0]=endh; allarray[i][1][1]=endw; } for(var i=0;i"; } document.getElementById("dv").innerHTML=res; }《script》