Javascript two-dimensional array definition and initialization Methods

Source: Internet
Author: User
Tags array definition
This article mainly summarizes three methods for defining and initializing two-dimensional arrays of js. If you need them, you can refer to them and hope to help you. Method 1: define and initialize them directly, you can use
Var _ TheArray = [["0-1", "0-2"], ["1-1", "1-2"], ["2-1 ", "2-2"]

Method 2: Two-dimensional array with unknown length

Var tArray = new Array (); // declare a dimension first for (var k = 0; k
 
  

Input the required value to the defined array.
TArray [6] [1] = 5; // You can pass the value of 5 to the array to overwrite the initialization null.

Method 3: before that, the above two methods have problems. method 2, each definition is initialized. Although it can be modified dynamically later, it is still not feasible.

So I tried a method to dynamically pass values to the array.

Ps: array interesting phenomena encountered in practice

We thought that two-dimensional arrays could directly pass in values like the following.

For (var a = 0;
   
    

The result is that the value of the next array is received in tArray [a], and the content of matArray [a] is ignored. If you change the position, matArray [a] is behind, the value of addArray [a] is passed in.

Thinking: simple example:

The Code is as follows:


Var a = [1, 2];

Var B = [];

B [0] = a; // pass array a as an element of array B to array B

Alert (B [0] [1]); // 2


The above is the simplest two-dimensional array,

The above example is written in another way:

The Code is as follows:


Var B = [];

B [0] = [1, 2]; // pass the array [1, 2] into array B as an element of array B

Alert (B [0] [1]); // 2


We can see that the above B [0] = [1, 2] is usable.

The Code is as follows:


For (var a = 0;

TArray [a] = [matArray [a], addArray [a]; Modify () in the preceding example to [] to form a two-dimensional array.

};


Conclusion: method 3:

The Code is as follows:


For (var a = 0;

TArray [a] = [aArray [a], bArray [a], cArray [a]; you can also add dArray [a], eArray [a]

};


This situation applies to the situation where several arrays are known and combined into a two-dimensional array.

Create multi-dimensional arrays in JS

 《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";  }  document.getElementById("dv").innerHTML=res;  }《script》

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.