Summary of three methods of js 2D array definition and initialization

Source: Internet
Author: User
Tags array definition

Method 1: define and initialize 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
Copy codeThe Code is as follows:
Var tArray = new Array (); // declare a dimension first
For (var k = 0; k <I; k ++) {// One-dimensional length is I, and I is a variable, which can be changed according to the actual situation

TArray [k] = new Array (); // declare two-dimensional. An element in each one-dimensional Array is an Array;

For (var j = 0; j <p; j ++) {// The number of p that each element array can contain in a one-dimensional array. p is also a variable;

TArray [k] [j] = ""; // initialize the variable here. The initialization here is empty, and the values in it are overwritten with the required values.
}
}

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.
Copy codeThe Code is 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 arrays are directly transmitted to tArray [].

};

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:
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe Code is as follows:
For (var a = 0; a <I; a ++ ){

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

};

Conclusion: method 3:
Copy codeThe Code is as follows:
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 applies to the situation where several arrays are known and combined into a two-dimensional array.

Related Article

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.