我正在尝试创建一个二维数组,但出现此错误。
我循环一个对象并尝试分配它们,但它不会让我为第二维分配一个值。
这就是我所拥有的:
//this is globally set
var gcollision = new Array();
function create() {
for (var X in sdata) {
X = parseInt(X);
for (var Y in sdata[X]) {
Y = parseInt(Y);
width = parseInt(sdata[X][Y][2]);
height = parseInt(sdata[X][Y][3]);
for (i = X; i != X + width; i++) {
//error occurs here "Uncaught TypeError: Cannot set property '3' of undefined"
gcollision[i][Y] = 1
for (j = Y; j != Y + height; j++) {
gcollision[X][j] = 1
}
}
}
}
如何使它正确设置值?
编辑 sdata 如下所示:
var sdata = {"4":{"7":["1","7","3","3"]},"3":{"3":["2","8","1","1"]}};