JavaScript 多维数组

在 JavaScript 中,多维数组在其内部包含另一个数组

这是一个多维数组的简单示例。阅读本教程的其余部分以了解更多信息。

示例

// multidimensional array // contains 3 separate arrays as elements const data = [[1, 2, 3], [1, 3, 4], [4, 5, 6]];
console.log(data); // Output : [ [ 1, 2, 3 ], [ 1, 3, 4 ], [ 4, 5, 6 ] ]

在这里,我们创建了一个名为 data 的多维数组,其元素为数组:[ 1, 2, 3 ][ 1, 3, 4 ][ 4, 5, 6 ]


使用现有数组作为元素

我们也可以通过将现有数组嵌套在其中来创建多维数组。例如,

// declare three arrays
let student1 = ['Jack', 24];
let student2 = ['Sara', 23];
let student3 = ['Peter', 24];

// create multidimensional array // using student1, student2, and student3 let studentsData = [student1, student2, student3];
// print the multidimensional array console.log(studentsData); // Output: [ [ 'Jack', 24 ], [ 'Sara', 23 ], [ 'Peter', 24 ] ]

在这里,我们首先创建了三个名为 student1student2student3 的数组。

然后,我们将这三个数组嵌套在 studentsData 数组中,以创建我们的多维数组

let studentsData = [student1, student2, student3];

访问数组元素

您可以使用数组索引来访问多维数组的元素。例如,

let x = [
['Jack', 24],
['Sara', 23], 
['Peter', 24]
];

// access the first item console.log(x[0]); // [ 'Jack', 24 ] // access the first item of the first inner array console.log(x[0][0]); // Jack // access the second item of the third inner array console.log(x[2][1]); // 24

输出

[ 'Jack', 24 ]
Jack
24

您可以将多维数组(在本例中为 x)视为一个具有3行和2列的表。

Accessing multidimensional array elements
访问多维数组元素

向多维数组添加元素

您可以使用索引表示法或 push() 方法向多维数组添加元素。

1. 使用索引表示法

let studentsData = [["Jack", 24], ["Sara", 23]];

// add "hello" as the 3rd element // of the 2nd inner array studentsData[1][2] = "hello";
console.log(studentsData); // Output: [ [ 'Jack', 24 ], [ 'Sara', 23, 'hello' ] ]

2. 使用 push() 方法

push() 方法将元素插入数组的末尾。例如,

let studentsData = [["Jack", 24], ["Sara", 23]];

// add element to the end of the outer array studentsData.push(["Peter", 24]);
console.log(studentsData);
// add "hello" as the final element // of the 2nd inner array studentsData[1].push("hello");
console.log(studentsData);

输出

[ [ 'Jack', 24 ], [ 'Sara', 23 ], [ 'Peter', 24 ] ]
[ [ 'Jack', 24 ], [ 'Sara', 23, 'hello' ], [ 'Peter', 24 ] ]
使用 splice() 方法添加元素。

您还可以使用 splice() 方法在指定索引处添加元素。例如,

let studentsData = [["Jack", 24], ["Sara", 23]];

// add element at 1 index studentsData.splice(1, 0, ["Peter", 24]);
console.log(studentsData); // Output: [ [ 'Jack', 24 ], [ 'Peter', 24 ], [ 'Sara', 23 ] ]

从多维数组中删除元素

您可以使用 splice() 方法从多维数组的任何位置删除元素。例如,

let studentsData = [['Jack', 24], ['Sara', 23],];

// remove one element // starting from index 0 studentsData.splice(0,1);
console.log(studentsData); // Output: [ [ 'Sara', 23 ] ]

在上面的程序中,studentsData.splice(0,1) 删除了多维数组的第一个元素。这里,

  • 0 - 要修改数组的起始索引。
  • 1 - 要删除的元素数量。

如果要删除两个数组,可以使用代码 studentsData.splice(0,2)

使用 pop() 方法删除最后一个元素。

如果要从多维数组中删除最后一个元素,可以使用 pop() 方法。

删除外层数组中的元素

let studentsData = [["Jack", 24], ["Sara", 23]];

// remove the array element from outer array
studentsData.pop();

console.log(studentsData);

// Output: [ [ 'Jack', 24 ] ]

删除内层数组中的元素

// remove the element from the inner array
let studentsData = [['Jack', 24], ['Sara', 23]];
studentsData[1].pop();

console.log(studentsData); 

// Output: [ [ 'Jack', 24 ], [ 'Sara' ] ]

遍历多维数组

在 JavaScript 中,您可以使用嵌套循环来遍历多维数组:一个循环用于外层数组,另一个循环在其中用于内层数组。例如,

let studentsData = [["Jack", 24], ["Sara", 23]];

// loop over outer array
for(let i = 0; i < studentsData.length; i++) {

    // loop over inner array elements
    for(let j = 0; j < studentsData[i].length; j++) {
        console.log(studentsData[i][j]);
    }
}

输出

Jack
24
Sara
23
使用 forEach() 遍历多维数组

您还可以使用 数组的 forEach() 方法 来遍历多维数组。例如,

let studentsData = [["Jack", 24], ["Sara", 23]];

// iterate over studentsData
studentsData.forEach((student) => {
    student.forEach((data) => {
        console.log(data);
    });
});

输出

Jack
24
Sara
23

第一个 forEach() 方法用于遍历外层数组的元素,第二个 forEach() 用于遍历内层数组的元素。

使用 for...of 遍历多维数组

您可以使用 for...of 循环来遍历多维数组。例如,

let studentsData = [["Jack", 24], ["Sara", 23]];

for (let i of studentsData) {
  for (let j of i) {
    console.log(j);
  }
}

输出

Jack
24
Sara
23

在我们结束之前,让我们用一个挑战来检验您对 JavaScript 多维数组的知识!您能解决下面的挑战吗?

挑战

编写一个函数来展平嵌套数组。

  • 展平数组是指将多维数组转换为一维数组的过程,同时保持元素的顺序。
  • 例如,对于输入 [[10, 20], [30], [40, 50, 60]],展平后的输出是 [10, 20, 30, 40, 50, 60]
  • 给定一个多维数组,返回其展平后的版本。
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战