Copy the code code as follows:
var a= new Array(new Array(1,2),new Array('b','c'));
document.write(a[1][1]);
To put it bluntly, it 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(); //Define one-dimensional array
for(i=0;i<nRow;i++){
//Define each sub-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 regarded 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 line part can also be implemented using the push() method of the js Array built-in object, because when arr1.push(arr2), the entire array arr2 will be added to the arr1 array as an element, so the for loop in the dotted line It can be completely replaced with the following statement: array1[i].push(new Array(nColumn));
Today I also discovered that it can also be defined like this or made into a two-dimensional array;
Copy the code code as follows:
var a= new Array(new Array(1,2),new Array('b','c'));
document.write(a[1][1]);
ps: Pay attention to the difference between push and concat!
The push method will add new elements in the order 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 combine elements from two or more arrays, use the concat method.
The concat method returns an Array object containing concatenations of array1 and any other items provided. The items to be added (item1...itemN) will be added to the array in order from left to right. 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.
Very good! ! !