Js handles methods that cannot define two dimensional arrays by themselves

  • 2020-03-30 02:10:38
  • OfStack


var a= new Array(new Array(1,2),new Array('b','c')); 
document.write(a[1][1]); 
 To put it bluntly, use it for Loop defines a two-dimensional array!  
?
<script language="javascript" type="text/javascript"> 
    function Array_2(nRow,nColumn){ 
        var array1=new Array(); //Define a one-dimensional array
        for(i=0;i<nRow;i++){ 
                       //Each child element is defined 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 second 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 part can also be implemented by using the push() method of the built-in object of js Array, because arr1.push(arr2) will add the entire Array arr2 as an element to the arr1 Array, so the for loop in the dotted line can be completely replaced with the following statement: array1[I]. Push (new Array(nColumn));    

And it turns out today that you can also define it this way or you can make it a two-dimensional array;


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!

A push Method adds the elements in the order in which they appear. If one of the arguments is an array, the array is added to the array as a single element. If you want to merge elements in two or more arrays, use the concat method.

concat Method returns an Array object that contains the connection to array1 and any other items provided. Item to be added (item1... ItemN) is added to the array in left-to-right order. If an item is an array, add its contents to the end of array1. If the item is not an array, it is added to the end of the array as a single array element.

Great!!


Related articles: