Js two dimensional array definition and initialization of three methods summary

  • 2020-03-30 02:10:33
  • OfStack

Method 1: define and initialize directly, which can be used in case of few encounters

Var _TheArray = [[" 0-1 "and" 0-2 "], [1 - "1", "1-2"], [2-1 ", "2-2"]]

Method 2: two-dimensional array of unknown length


var tArray = new Array(); //So let's declare one dimension
for(var k=0;k<i;k++){ //The length of one dimension is I, and I is a variable, which can be changed according to the actual situation
tArray[k]=new Array(); //Declare that each element in a one-dimensional array is an array;
for(var j=0;j<p;j++){ //The amount of p that each element of a one-dimensional array can contain. P is also a variable;
tArray[k][j]=""; //I'm going to initialize the variable here, and I'm going to initialize it to null, and then I'm going to overwrite it with the values I want
 }
}

Pass in the required values to the defined array
TArray [6] [1] = 5; // this passes the value of 5 into the array, overwriting the null initialization

Method 3: before this, the above two methods have problems, method 2, each definition is initialized, although later can be dynamically modified, but still not method

So I tried a way to dynamically pass values into arrays

Ps: some interesting things about arrays in practice

I thought a two-dimensional array could pass in values directly like this


for(var a=0;a<i;a++){
tArray[a]=(matArray[a],addArray[a]); //MatArray [a] and addArray[a] are two arrays that are passed directly into tArray[a]

};

The result is that the values received in tArray[a] are the values of the following array, the contents of matArray[a] are ignored, and the values of addArray[a] are passed in if the values of matArray[a] are at the end of a different position.

Think: a simple example:


var a=[1,2];
var b=[];
b[0]=a;//Pass the array a into the b array as an element of the b array
alert(b[0][1]); //2

This is the simplest two-dimensional array,

The above example is written differently:


var b=[];
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

You can see that b[0]=[1,2] up here works


for(var a=0;a<i;a++){
tArray[a]=[ matArray[a],addArray[a] ];  The () in the above example is changed to []  I can successfully form a two-dimensional array 
};

Conclusion: method 3:


for(var a=0;a<i;a++){
tArray[a]=[ aArray[a],bArray[a],cArray[a]];  You can add dArray[a],eArray[a]
};

This is true if you have several arrays, and you combine them into a two-dimensional array

JS creates a 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 first "+i+" The starting coordinate of "+sh+","+sw+" The ending coordinate is: "+eh+","+ew+"<br/>";
 }
 document.getElementById("dv").innerHTML=res;
 }
</script>

Supplementary information:

These two days when doing the project, we need to pass a two-dimensional array with string key name through js, but when we pass it to the background, we get false, which fails in many ways.

And how to pass an array with a string key name in Ajax

One-dimensional array:

One dimensional arrays can be named with Numbers and strings.

Var data = []; // I don't know the number
Var data = new Array(); // I 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:

A:


var data = [];
data.push(['a']);

or


var data_1 = ['a'];d
ata[0]=data_1;

2:


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, js arrays must be numeric key names.

If you want to use a string as a name, use the form of an object:


var data ={
'a':{'id':1,'url':h}
};

This article introduces this.

JS two-dimensional array definition and length judgment

Dynamic definition of two-dimensional array:

1. First define one dimension:

Var arr = new Array();

2. Definition of 2d:

Arr [0] = new Array ();

Arr [1] = new Array ();

3. Assign values to arrays:

Arr [0] [0] = "00";

Arr [0] [1] = "01";

Arr [1] [0] = "10";

Arr [1] [1] = "11";

4. Determine the array length:

Number of rows in a two-dimensional array: arr.length

The number of columns in the corresponding row: arr[0].length // the number of columns in the corresponding row: arr[0].length //

5. Array call:

Var STR = arr[0][0];


Related articles: