改变多维数组的值

时间:2013-06-05 08:56:05

标签: javascript arrays canvas multidimensional-array onclick

我想更改多维数组的值,但这段代码一直告诉我 “无法设置属性'5'(这是我猜的数组的长度)未定义的”

obj.figures = new Array(
new Array(6,5,5,5,6,5,5),
new Array(5,0,0,0,5,0,0),
new Array(5,5,5,5,5,5,5),
new Array(0,1,1,1,0,2,2),
new Array(0,1,0,1,0,2,0)
);

function change() {
for(var y = 0; y<obj.figures.length; y++) {
    for(var x = 0; x<obj.figures[y].length; x++) {
        $(y+","+x).addEventListener("click", function() {
            console.log(this.id);
            obj.figures[y][x] = 5;
            }, false);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您必须在此声明中切换y和x值:

obj.figures[y][x] = 5;

应该是

obj.figures[x][y] = 5;