[[2], [12], [2]];
这不是维度的声明,而是四个数组文字。JS 中没有多维数组。它们只是可以包含任意值(包括其他列表)的一维列表。
要创建和填充包含其他数组的数组,您必须使用以下内容:
var initData = []; // an empty array
for (var i = 0; i < 2; i++) {
initData[i] = []; // that is filled with arrays
for (var j = 0; j < 12; j++) {
initData[i][j] = []; // which are filled with arrays
for (var k = 0; k < 2; k++) {
initData[i][j][k] = -1; // which are filled with numbers
}
}
}
或者,应用你的循环展开:
var initData = [[], []]; // an array consisting of two arrays
for (var i = 0; i < 12; i++) {
// which each are filled with arrays that consist of two numbers
initData[0][i] = [-1, -1];
initData[1][i] = [-1, -1];
}