多维数组未直接在JavaScript中提供。如果要使用任何充当多维数组的东西, 则需要使用另一个一维数组来创建多维数组。因此, JavaScript中的多维数组被称为另一个数组中的数组。我们需要将一些数组放入一个数组中, 然后整个事情就像多维数组一样工作。其他数组将插入其中的数组, 该数组在我们的代码中用作多维数组。定义多维数组与定义普通一维数组完全相同。
一维数组:
var arr = []; // Empty 1D array
var arr1 = ["A", "B", "C", "D"] // 1D array contains some alphabets
var arr1 = [1, 2, 3, 4, 5] // 1D array contains some digits
多维数组:
方法1:
1st, need to define some 1D array
var arr1 = ["ABC", 24, 18000];
var arr2 = ["EFG", 30, 30000];
var arr3 = ["IJK", 28, 41000];
var arr4 = ["EFG", 31, 28000];
var arr5 = ["EFG", 29, 35000];
// "salary" defines like a 1D array but it already contains some 1D array
var salary = [arr1, arr2, arr3, arr4, arr5];
这里arr1, arr2, …arr5是里面的一些一维数组薪水数组。
方法2:
var salary = [
["ABC", 24, 18000], ["EFG", 30, 30000], ["IJK", 28, 41000], ["EFG", 31, 28000], ];
这里, 薪水数组的工作方式类似于多维数组。这种表示法称为数组文字。
访问薪水数组的元素:
- 要访问数组元素, 我们需要一个基于索引的简单表示法
// This notation access the salary of "ABC" person which is 18000, // [0] selects 1st row, and [2] selects the 3rd element // of that 1st row which is 18000 salary[0][2]; // Similarly, salary[3][2]; // Selects 28000 **This notation is used for both Method 1 and Method 2.
- 对于许多迭代, 我们需要使用循环来访问元素,
// This loop is for outer array for (var i = 0, l1 = salary.length; i < l1; i++) { // This loop is for inner-arrays for (var j = 0, l2 = salary[i].length; j < l2; j++) { // Accessing each elements of inner-array documents.write( salary[i][j] ); } }
在多维数组中添加元素:多维数组中的元素添加可以通过以下两种方式实现:内部数组or外层阵列。内部数组可以通过两种不同的方式完成。
- 向内部数组添加元素:
- 我们可以使用简单方括号在多维数组中添加元素的符号。
salary[3][3] = "India"; // It adds "India" at the 4th index of 4th sub-array, // If we print the entire 4th sub-array, document.write(salary[3]); // the output will be : ["EFG", 31, 28000, "India"] // indexing starts from 0
- 我们可以用推()在数组中添加元素的方法。
salary[3].push("India", "Mumbai"); // It add "India" at the 4th index and "Mumbai" at // 5th index of 4th sub-array // If we print the entire 4th sub-array, // document.write(salary[3]); // The output will be : ["EFG", 31, 28000, "India", "Mumbai"] // Indexing starts from 0
- 我们可以使用简单方括号在多维数组中添加元素的符号。
- 向外部数组添加元素:它与以前的方法非常相似。
salary.push(["MNO", 29, 33300]); // This row added after the last row in the "salary" array
删除多维数组中的元素:我们可以用pop()从内部数组中删除元素的方法, 也可以使用pop()删除整个内部数组的方法。
// Remove last element from 4th sub-array
// That is 28000 indexing starts from 0
salary[3].pop();
// Removes last sub-array
// That is "["EFG", 31, 28000]"
salary.pop();
范例1:
// Prints a simple multidimensional array in JavaScript
<script>
var arr1 = [ "ABC" , 24, 18000];
var arr2 = [ "EFG" , 30, 30000];
var arr3 = [ "IJK" , 28, 41000];
var arr4 = [ "EFG" , 31, 28000];
var arr5 = [ "EFG" , 29, 35000];
var salary = [arr1, arr2, arr3, arr4, arr5];
for ( var i = 0; i < salary.length; i++) {
document.write(salary[i] + "<br>" );
}
</script>
输出如下:
ABC, 24, 18000
EFG, 30, 30000
IJK, 28, 41000
EFG, 31, 28000
EFG, 29, 35000
范例2:
// Prints a simple multidimensional array in
// JavaScript with different declaration
<script>
var salary = [
[ "ABC" , 24, 18000], [ "EFG" , 30, 30000], [ "IJK" , 28, 41000], [ "EFG" , 31, 28000], ];
for ( var i = 0; i < salary.length; i++) {
document.write(salary[i] + "<br>" );
}
</script>
输出如下:
ABC, 24, 18000
EFG, 30, 30000
IJK, 28, 41000
EFG, 31, 28000
EFG, 29, 35000
范例3:
// Prints a simple multidimensional array in JavaScript
// where we just print the salary of a specific person
<script>
var salary = [
[ "ABC" , 24, 18000], [ "EFG" , 30, 30000], [ "IJK" , 28, 41000], [ "EFG" , 31, 28000], ];
document.write( "salary of 2nd person : " + salary[1][2] + "<br>" );
document.write( "salary of 4th person : " + salary[3][2] + "<br>" );
</script>
输出如下:
salary of 2nd person : 30000
salary of 4th person : 28000
范例4:
// Prints a simple multidimensional array in
// JavaScript where we add elements in the array
// using simple square bracket and push() method
<script>
var salary = [
[ "ABC" , 24, 18000], [ "EFG" , 30, 30000], [ "IJK" , 28, 41000], [ "EFG" , 31, 28000], ];
// Prints first array
document.write( "Original array :<br>" );
for ( var i = 0; i < salary.length; i++) {
document.write(salary[i] + "<br>" );
}
// Adding "India" at the 4th index of 4th sub array
salary[3][3] = "India" ;
document.write( "<br>after adding \"India\" at the 4th array :<br>" );
for ( var i = 0; i < salary.length; i++) {
document.write(salary[i] + "<br>" );
}
document.write( "<br>after adding \"USA\" and \"Canada\" "
+ "at the 3rd array using \"push()\" method :<br>" );
salary[2].push( "USA" , "Canada" );
// Adding "USA" and "Canada" in the 2nd sub-array
for ( var i = 0; i < salary.length; i++) {
document.write(salary[i] + "<br>" );
}
</script>
输出如下:
Original array :
ABC, 24, 18000
EFG, 30, 30000
IJK, 28, 41000
EFG, 31, 28000
after adding "India" at the 4th array :
ABC, 24, 18000
EFG, 30, 30000
IJK, 28, 41000
EFG, 31, 28000, India
after adding "USA" and "Canada" at the 3rd array using "push()" method :
ABC, 24, 18000
EFG, 30, 30000
IJK, 28, 41000, USA, Canada
EFG, 31, 28000, India
范例5:
// Prints a simple multidimensional array in
// JavaScript where we add a new inner array
<script>
var salary = [
[ "ABC" , 24, 18000], [ "EFG" , 30, 30000], [ "IJK" , 28, 41000], [ "EFG" , 31, 28000], ];
// Prints first array
document.write( "Original array :<br>" );
for ( var i = 0; i < salary.length; i++) {
document.write(salary[i] + "<br>" );
}
document.write( "<br>After adding a new inner array :<br>" );
// Pushing a new sub-array
salary.push([ "MNO" , 29, 33300]);
for ( var i = 0; i < salary.length; i++) {
document.write(salary[i] + "<br>" );
}
</script>
输出如下:
Original array :
ABC, 24, 18000
EFG, 30, 30000
IJK, 28, 41000
EFG, 31, 28000
After adding a new inner array :
ABC, 24, 18000
EFG, 30, 30000
IJK, 28, 41000
EFG, 31, 28000
MNO, 29, 33300
范例6:
// Prints a simple multidimensional array in
// JavaScript where we remove a single element
//and a entire sub-array
<script>
var salary = [
[ "ABC" , 24, 18000], [ "EFG" , 30, 30000], [ "IJK" , 28, 41000], [ "EFG" , 31, 28000], ];
// Prints first array
document.write( "Original array :<br>" );
for ( var i = 0; i < salary.length; i++) {
document.write(salary[i] + "<br>" );
}
document.write( "<br>After removing last element "
+ "of last inner array :<br>" );
// Removes the last element of 3rd sub-array
salary[3].pop();
for ( var i = 0; i < salary.length; i++) {
document.write(salary[i] + "<br>" );
}
document.write( "<br>After removing last inner array :<br>" );
// Removes last sub-array
salary.pop();
for ( var i = 0; i < salary.length; i++) {
document.write(salary[i] + "<br>" );
}
</script>
输出如下:
Original array :
ABC, 24, 18000
EFG, 30, 30000
IJK, 28, 41000
EFG, 31, 28000
After removing last element of last inner array :
ABC, 24, 18000
EFG, 30, 30000
IJK, 28, 41000
EFG, 31
After removing last inner array :
ABC, 24, 18000
EFG, 30, 30000
IJK, 28, 41000