JS processing of its own can not define a two-dimensional array of methods detailed

Source: Internet
Author: User
Tags arrays dashed line

  This article is mainly on the JS processing itself can not define a two-dimensional array of methods to introduce, the need for friends can come to the reference, hope to read everyone has helped

  Code as follows: Var a= new Array (new array (1,2), new array (' B ', ' C '));  document.write (a[1][1));  plainly, is to 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 (); Definition one-dimensional array           for (i=0;i<nrow;i++) {                         //Each child element is also defined as an array               array1[i]=new Array ();  //----------------------------------------  for (n=0;n<ncolumn;n++) {        &NB Sp         array1[i][n] = '; At this point Aa[i][n] can be viewed as a two-level array              } //------------------------------------ -- }          return array1;     }        var ar ray_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>   //Dashed section can also be used JS The push () method of the array built-in object is implemented 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 completely replaced with the following statement: Array1[i].push (New Array (Ncolumn));      Today also found that this definition can also be made into two-dimensional arrays; the   code is 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!!!  

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.