Method 1: Directly define and initialize. This can be used when the number is small.
var _TheArray = [["0-1","0-2"],["1-1","1-2"],["2-1","2-2"]]
Method 2: Two-dimensional array of unknown length
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]=""; //这里将变量初始化,我这边统一初始化为空,后面在用所需的值覆盖里面的值 } }
Pass the required values into the defined array
tArray[6][1]=5; //In this way, the value of 5 can be passed into the array, overwriting the initialized empty
Method three: Before this, both of the above methods had problems. Method two, each definition is initialized. Although it can be modified dynamically later, it still does not work
So I tried a method of dynamically passing values into an array
ps: Some interesting phenomena encountered with arrays in practice
I originally thought that the two-dimensional array can directly pass in the value as follows
for(var a=0;a<i;a++){ tArray[a]=(matArray[a],addArray[a]); //matArray[a]和addArray[a]是两个数组,这两个数组直接传入tArray[a]中 };
The result is that what is received in tArray[a] is the value of the next array, and the content of matArray[a] is ignored. If the position is changed and matArray[a] is behind, then addArray[a] is passed in. ] value.
Think: Simple example:
var b=[];
b[0]=a;//Pass array a into array b as an element of array b
alert(b[0][1]); //2
Another way to write the above example:
b[0]=[1,2];//Pass the array [1,2] into the b array as an element of the b array
alert(b[0][1]); //2
tArray[a]=[ matArray[a],addArray[a] ]; In the above example, () is changed to [] to successfully form a two-dimensional array
};
tArray[a]=[ aArray[a],bArray[a],cArray[a]]; You can also add dArray[a],eArray[a]
};
JS creates multi-dimensional array
<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>