JavaScript 多维数组

x33g5p2x  于2022-04-22 转载在 Java  
字(3.0k)|赞(0)|评价(0)|浏览(283)

在本教程中,您将借助示例了解 JavaScript 多维数组。
    多维数组是包含另一个数组的数组。例如,

// multidimensional array
const data = [[1, 2, 3], [1, 3, 4], [4, 5, 6]];
创建多维数组

这是在 JavaScript 中创建多维数组的方法。
    示例 1

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

示例 2

let student1 = ['Jack', 24];
let student2 = ['Sara', 23];
let student3 = ['Peter', 24];

// multidimensional array
let studentsData = [student1, student2, student3];

在这里,示例 1 和示例 2 都创建了一个具有相同数据的多维数组。

访问数组的元素

您可以使用索引 (0, 1, 2 …) 访问多维数组的元素。例如,

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

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

访问多维数组元素

将元素添加到多维数组

可以使用数组的 push()方法或索引符号向多维数组中添加元素。

将元素添加到外部数组
let studentsData = [['Jack', 24], ['Sara', 23],];
studentsData.push(['Peter', 24]);

console.log(studentsData); //[["Jack", 24], ["Sara", 23], ["Peter", 24]
将元素添加到内部数组
// using index notation
let studentsData = [['Jack', 24], ['Sara', 23],];
studentsData[1][2] = 'hello';

console.log(studentsData); // [["Jack", 24], ["Sara", 23, "hello"]]
// using push()
let studentsData = [['Jack', 24], ['Sara', 23],];
studentsData[1].push('hello');

console.log(studentsData); // [["Jack", 24], ["Sara", 23, "hello"]]

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

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

// adding element at 1 index
studentsData.splice(1, 0, ['Peter', 24]);

console.log(studentsData); // [["Jack", 24], ["Peter", 24], ["Sara", 23]]
从多维数组中删除元素

可以使用数组的 pop()方法从多维数组中删除元素。例如,

从外部数组中删除元素
// remove the array element from outer array
let studentsData = [['Jack', 24], ['Sara', 23],];
studentsData.pop();

console.log(studentsData); // [["Jack", 24]]
从内部数组中删除元素
// remove the element from the inner array
let studentsData = [['Jack', 24], ['Sara', 23]];
studentsData[1].pop();

console.log(studentsData); // [["Jack", 24], ["Sara"]]

您还可以使用 splice() 方法删除指定索引处的元素。例如,

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

// removing 1 index array item
studentsData.splice(1,1);
console.log(studentsData); // [["Jack", 24]]
迭代多维数组

您可以使用多维数组的 forEach()方法迭代多维数组。例如,

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

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

输出

Jack
24
Sara
23

第一个 forEach()方法用于迭代外部数组元素,第二个 forEach()方法用于迭代内部数组元素。
    您还可以使用 for…of 循环来迭代多维数组。例如,

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

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

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

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

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

    // get the length of the inner array elements
    let innerArrayLength = studentsData[i].length;
    
    // looping inner array elements
    for(let j = 0; j < innerArrayLength; j++) {
        console.log(studentsData[i][j]);
    }
}

上一教程 :JS Array                                          下一教程 :JS Strings

参考文档

[1] Parewa Labs Pvt. Ltd. (2022, January 1). Getting Started With JavaScript, from Parewa Labs Pvt. Ltd: https://www.programiz.com/javascript/multidimensional-array

相关文章