Method one: Directly defined and initialized, this encounter a small number of cases can be used
var _thearray = [["0-1", "0-2"],["1-1", "1-2"],["2-1", "2-2"]]
Method Two: Two-dimensional array of unknown length
| 12345678910 |
var tArray = new Array(); //先声明一维for(var k=0;k<i;k++){ //一维长度为i,i为变量,可以根据实际情况改变 tArray[k]=new Array(); //声明二维,每一个一维数组里面的一个元素都是一个数组;for(var j=0;j<p;j++){ //一维数组里面每个元素数组可以包含的数量p,p也是一个变量;tArray[k][j]=""; //这里将变量初始化,我这边统一初始化为空,后面在用所需的值覆盖里面的值 }} |
Passing the desired value to the defined array
tarray[6][1]=5;//This allows the value of 5 to be passed into the array, overwriting the initialized null
Method Three: Before this, both methods have problems, method two, each definition is initialized, although the latter can be modified dynamically, but still no method
So I tried a way to dynamically pass in a value to an array
PS: Some interesting phenomena encountered during the practice of the array
Would have thought that a two-dimensional array could pass directly to the value as follows
| 1234 |
for(vara=0;a<i;a++){tArray[a]=(matArray[a],addArray[a]); //matArray[a]和addArray[a]是两个数组,这两个数组直接传入tArray[a]中}; |
The result is that Tarray[a] receives the value of the subsequent array, the contents of Matarray[a] are ignored, and if a position is changed, Matarray[a] is followed, the value of Addarray[a] is passed in.
Thinking: A simple example:
The code is as follows:
var a=[1,2];
var b=[];
b[0]=a;//passing array A as an element of B array into the B array
Alert (b[0][1]); 2
Above is the simplest two-dimensional array,
The above example changes the way:
The code is as follows:
var b=[];
b[0]=[1,2];//the array as an element of the B array into the B array
Alert (b[0][1]); 2
You can see that the above b[0]=[1,2] is available.
The code is as follows:
for (Var a=0;a<i;a++) {
tarray[a]=[Matarray[a],addarray[a]]; The above example () is modified to [] to make a two-dimensional array successfully.
};
Summary: Method Three:
The code is as follows:
for (Var a=0;a<i;a++) {
tarray[a]=[Aarray[a],barray[a],carray[a]]; You can also add darray[a],earray[a]
};
This applies to a number of known arrays, combining them into a two-dimensional array case
JS Creating multidimensional arrays
| 123456789101112131415161718192021222324252627282930 |
<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<allarray.length;i++) { var sh=allarray[i][0][0]; var sw=allarray[i][0][1] var eh=allarray[i][1][0]; var ew=allarray[i][1][1] res+="第"+i+"个坐标的开始坐标是:"+sh+","+sw+"结束坐标是:"+eh+","+ew+"<br/>"; } document.getElementById("dv").innerHTML=res; }</script> |
JavaScript--defining a two-dimensional array