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(); //First declare one-dimensional for(var k=0;k<i;k++){ //The length of one-dimensional is i, i is a variable, which can be changed according to the actual situation tArray[k]= new Array(); //Declare two dimensions, each element in a one-dimensional array is an array; for(var j=0;j<p;j++){ //The number p that each element array in a one-dimensional array can contain, p is also a variable; tArray[k][j]=""; //The variables are initialized here. I initialize them to empty here and use them later. The required value overrides the value inside}}
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 way to dynamically pass in values to an array
ps: Some interesting phenomena with arrays encountered 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] and addArray[a] are two arrays, these two The array is passed directly into 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, the value of addArray[a] is passed in. .
Think: Simple example:
var a=[1,2];var b=[];b[0]=a;//Pass array a into array b as an element of array b alert(b[0][1]); // 2
The above is the simplest two-dimensional array,
Another way to write the above example:
var b=[];b[0]=[1,2];//Pass array [1,2] as an element of b array into b array alert(b[0][1]); //2
It can be seen that the above b[0]=[1,2] can be used
for(var a=0;a<i;a++){tArray[a]=[ matArray[a],addArray[a] ]; In the above example, () can be changed to [] to successfully form a two-dimensional array. };
Summary: Method three:
for(var a=0;a<i;a++){tArray[a]=[ aArray[a],bArray[a],cArray[a]]; You can also add dArray[a],eArray[a]};
This situation is applicable to the case where several arrays are known and combined into a two-dimensional array.
JS creates multidimensional 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+="The starting coordinates of the "+i+"th coordinate are: "+sh+", "+sw+" and the ending coordinates are: "+eh+", "+ew+"<br/>"; } document.getElementById("dv ").innerHTML=res; }</script>
Supplementary information:
When I was working on a project in the past two days, I had to pass a two-dimensional array with string key names through js. However, when I passed it to the background, I got false. I tried many methods but it didn't work. Today I will introduce it: js array initialization question;
And how to pass an array with string key names under Ajax
One-dimensional array:
One-dimensional arrays can be named using numbers or strings.
var data = [];//I don’t know the number
var data = new Array(); //Don’t know the number
If you know the number, the specific value can be used:
var data = new Array(1);data['a'] = 'a';
or
var data= ['a'];
Two-dimensional array:
Two-dimensional arrays do not support string key names.
one:
var data = [];data.push(['a']);
or
var data_1 = ['a'];data[0]=data_1;
two:
var data=new Array();for(var i=0;i<2;i++){data[i]=new Array(); for(var j=0;j<2;j++){data[i] [j]=1;}
remind:
When using Ajax to pass data, the js array must be a numeric key name.
If you want to use a string to create a name, you need to use an object: as follows:
var data ={'a':{'id':1,'url':h}};
This article introduces this.
The definition and length judgment of JS two-dimensional array
Dynamically define a two-dimensional array:
1. Define one dimension first:
var arr = new Array();
2. Define two dimensions:
arr[0] = new Array();
arr[1] = new Array();
3. Assign values to the array:
arr[0][0] = "00";
arr[0][1] = "01";
arr[1][0] = "10";
arr[1][1] = "11";
4. Determine the array length:
The number of rows in the two-dimensional array: arr.length
The number of columns in the corresponding row: arr[0].length //The length of the first row is 2 based on the above example.
5. Array call:
Very simple: var str = arr[0][0];