JS handles its own inability to define a two-dimensional array of methods _javascript tips

Source: Internet
Author: User
Tags dashed line

Copy Code code as follows:

var a= new Array (new array (1,2), new array (' B ', ' C '));
document.write (a[1][1]);
To be blunt, use a For loop to define a two-dimensional array!
?
<script language= "javascript" type= "Text/javascript" >
function Array_2 (nrow,ncolumn) {
var array1=new Array (); Defining 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 point aa[i][n] can be thought of as a two-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 section can also be implemented using the push () method of the JS array built-in object, because Arr1.push (ARR2) will add the entire array arr2 as an element to the ARR1 array, so the for loop in the dashed line can be replaced with the following statement: Array1[i].push (New Array (Ncolumn));

It is also found today that the definition can also be made into two-dimensional arrays;

Copy Code code as follows:

var a= new Array (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 these elements in the order in which the new elements appear. if one of the arguments is an array, the array is added as a single element to the arrays. If you want to merge elements from two or more arrays, use the Concat method.

The concat method returns an Array object that contains a connection to the array1 and any other items provided. Items to add (item1 ... itemn) are added to the array in Left-to-right order. If an item is an array, add its contents to the end of the array1. If the item is not an array, it is added to the end of the array as a single array element.

Very good!!!

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.