Js processing methods that cannot define two-dimensional arrays themselves

Source: Internet
Author: User

Js processing methods that cannot define two-dimensional arrays themselves

This article mainly introduces how to deal with js processing methods that cannot define two-dimensional arrays. If you need them, please refer to them and hope to help you.

The Code is as follows:

Var a = new Array (1, 2), new Array ('B', 'C '));

Document. write (a [1] [1]);

To put it bluntly, we use the for loop to define a two-dimensional array!

?

<Script language = "javascript" type = "text/javascript">

Function Array_2 (nRow, nColumn ){

Var array1 = new Array (); // defines a one-dimensional Array

For (I = 0; I <nRow; I ++ ){

// Define each child element as an array

Array1 [I] = new Array ();

//----------------------------------------

For (n = 0; n <nColumn; n ++ ){

Array1 [I] [n] = ''; // at this time, aa [I] [n] can be considered as a list-level array.

}

//--------------------------------------

}

Return array1;

}

 

Var array_2 = Array_2 (3, 2 );

Array_2 [0] [1] = 1;

Array_2 [0] [2] = 2;

Array_2 [1] [1] = 3;

Array_2 [1] [2] = 4;

 

Document. write (array_2 [1] [2]);

</Script>

 

// The dotted line can also be implemented using the push () method of the js Array built-in object, because when arr1.push (arr2, the entire array arr2 is added to the arr1 array as an element. Therefore, the for loop in the dotted line can be replaced with the following statement: array1 [I]. push (new Array (nColumn ));

 

Today, we also found that the definition can also be made into a two-dimensional array;

The Code is as follows:

Var a = new Array (1, 2), new Array ('B', 'C '));

Document. write (a [1] [1]);

 

Ps: note the difference between push and concat!

 

The push method adds new elements in the order they appear. If one of the parameters is an array, the array will be added to the array as a single element. To merge two or more elements in an array, use the concat method.

 

The concat method returns an Array object that contains the connection between array1 and any other projects provided. Project to be added (item1... ItemN) is added to the array from left to right. If an item is an array, add its content to the end of array1. If the project is not an array, add it as a single array element to the end of the array.

 

Good !!!

 

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.