Home > Web Front-end > JS Tutorial > body text

Summary of three methods for defining and initializing js two-dimensional array_javascript skills

WBOY
Release: 2016-05-16 16:57:34
Original
2268 people have browsed it

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]="";    //这里将变量初始化,我这边统一初始化为空,后面在用所需的值覆盖里面的值
 }
}
Copy after login

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]中

};
Copy after login

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:

Copy code The code is as follows:

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:

Copy code The code is as follows:

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


It can be seen that the above b[0]=[1,2] can be used
Copy code The code is as follows:

for(var a=0;a

tArray[a]=[ matArray[a],addArray[a] ]; In the above example, () is changed to [] to successfully form a two-dimensional array

};


Summary: Method three:
Copy code The code is as follows:

for(var a=0;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 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>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template