## 什么是二维数组

``````[ a1, a2, a3, ..., an,
b1, b2, b3, ..., bn,
c1, c2, c3, ..., cn,
.
.
.
z1, z2, z3, ..., zn ]
``````

``````let twoDimensionalArr = [ [ a1, a2, a3, ..., an ],
[ b1, b2, b3, ..., bn ],
[ c1, c2, c3, ..., cn ],
.
.
.
[ z1, z2, z3, ..., zn ] ];
``````

``````let myArray = [
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 0, 0]
];
``````

``````let myArray = [
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9]
];
``````

## 为什么要在 JavaScript 中使用二维数组

• 它们允许有效地存储和操作大量的数据，例如在图像或视频处理、科学模拟和电子表格应用中。
• 二维数组还可以使用矩阵运算，如矩阵乘法和转置，这可以简化复杂的计算，使代码更易读。
• 二维数组可以表示线性代数中的数学矩阵和广泛的数据，如图形、地图和表格。
• 二维数组常用于涉及数据表、图像处理和游戏开发的应用中。

## 如何访问 JavaScript 二维数组中的元素

``````let MathScore = [
['John Doe', 20, 60, 'A'],
['Jane Doe', 10, 52, 'B'],
['Petr Chess', 5, 24, 'F'],
['Ling Jess', 28, 43, 'A'],
['Ben Liard', 16, 51, 'B']
];
``````

``````arrayName[rowIndex][columnIndex]
``````

``````console.log(MathScore[4][0]); // returns 'Ben Liard'
console.log(MathScore[2][1]); // returns 5
console.log(MathScore[1][3]); // returns 'B'
console.log(MathScore[2][2]); // returns 24
``````

### 如何访问一个二维数组的第一个和最后一个元素

``````console.log(MathScore[0][0]); // returns 'John Doe'
console.log(MathScore[MathScore.length-1][(MathScore[MathScore.length -1]).length - 1]); // returns 'B'
``````

### 如何将一个二维数组的所有元素相加

``````let numberArr = [
[10, 20, 60],
[8, 10, 52],
[15, 5, 24],
[26, 28, 43],
[12, 16, 51]
];

var sum = 0;
numberArr.forEach((row) => {
row.forEach((element) => {
sum += element;
});
});
console.log("The sum of all elements in the array is:" + sum); // returns "The sum of all elements in the array is: 380"
``````

## 如何在 JavaScript 中操作二维数组

### 如何在二维数组中插入一个元素

`push()` 方法将元素添加到二维数组的结尾，而 `unshift()` 方法将元素添加到二维数组的开头。

``````let MathScore = [
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
];

MathScore.push(["Tom Right", 30, 32, "B"]);

MathScore.unshift(["Alice George", 28, 62, "A"]);
``````

``````[
["Alice George", 28, 62, "A"],
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"],
["Tom Right", 30, 32, "B"]
]
``````

``````MathScore[0].push("B");
``````

``````let MathScore = [
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
];

MathScore.forEach((score) => {
let totalScore = score[1] + score[2];
score.push(totalScore);
});

console.log(MathScore);
``````

``````[
["John Doe", 20, 60, "A", 80],
["Jane Doe", 10, 52, "B", 62],
["Petr Chess", 5, 24, "F", 29],
["Ling Jess", 28, 43, "A", 71],
["Ben Liard", 16, 51, "B", 67]
]
``````

``````let MathScore = [
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
];

MathScore.splice(2, 0, ["Alice George", 28, 62, "A"]);
``````

``````[
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Alice George", 28, 62, "A"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
]
``````

### 如何从二维数组中删除一个元素

``````let MathScore = [
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
];

MathScore.pop();

MathScore.shift();
``````

``````[
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
]
``````

``````let MathScore = [
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
];

MathScore.forEach((score) => {
score.pop();
});

console.log(MathScore);
``````

``````[
["John Doe", 20, 60],
["Jane Doe", 10, 52],
["Petr Chess", 5, 24],
["Ling Jess", 28, 43],
["Ben Liard", 16, 51]
]
``````

``````let MathScore = [
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
];

MathScore.splice(2, 1);
``````

``````[
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
]
``````

### 如何使用数组字面量表示法创建一个二维数组

``````let arrayName = [
[ elements ],
[ elements ],
[ elements ], ... ];
``````

``````let MathScore = [
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
];
``````

### 如何使用嵌套 for 循环创建一个二维数组

``````let arr = [];
let rows = 4;
let columns = 3;

// creating two-dimensional array
for (let i = 0; i < rows; i++) {
arr[i] = [];
for (let j = 0; j < columns; j++) {
arr[i][j] = j;
}
}

console.log(arr);
``````

``````[
[0, 1, 2],
[0, 1, 2],
[0, 1, 2],
[0, 1, 2]
]
``````

``````let arr = [];
let rows = 4;
let columns = 3;

let value = 0;
// creating two-dimensional array
for (let i = 0; i < rows; i++) {
arr[i] = [];
for (let j = 0; j < columns; j++) {
arr[i][j] = value++;
}
}

console.log(arr);
``````

``````[
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]
]
``````

``````const create2Darr = (rows, columns) => {
let arr = [];
let value = 0;

// creating two-dimensional array
for (let i = 0; i < rows; i++) {
arr[i] = [];
for (let j = 0; j < columns; j++) {
arr[i][j] = value++;
}
}
console.log(arr);
};

let rows = 4;
let columns = 3;
create2Darr(rows, columns);
``````

## 如何在 JavaScript 中更新二维数组中的元素

``````let array = [1, 2, 3];
array[1] = 5;

console.log(array); // returns [1, 5, 3]
``````

``````let MathScore = [
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
];

MathScore[1] = ["Alice George", 28, 62, "A"];
console.log(MathScore);
``````

``````[
["John Doe", 20, 60, "A"],
["Alice George", 28, 62, "A"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
]
``````

``````let MathScore = [
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
];

MathScore[2][0] = "Jack Jim";
console.log(MathScore);
``````

``````[
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Jack Jim", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"]
]
``````