0

我正在循环我的对象数据,但得到了这个未定义的值。

不知道为什么,但希望有人能解释一下。

我的对象是这样的:

//globally set
var sdata = {"4":{"7":["1","7","3","3"]},"3":{"3":["2","8","1","1"]}};

我像这样循环对象:

function is_occupied(position) {
    for (var x in sdata) {
        for (var y in sdata) {
            // error's here Cannot read property '2' of undefined
            var ex = sdata[x][y][2] > position.block_width ? (sdata[x][y][2] + (sdata[x][y][2] - position.block_width)) : sdata[x][y][2],
            var ey = sdata[x][y][3] > position.block_height ? (sdata[x][y][3] + (sdata[x][y][3] - position.block_height)) : sdata[x][y][3];
            if (position.x >= sdata[x][y][2] && position.x <= ex && position.y >= sdata[x][y][3] && position.y <= ey) {
                alert('hit');
            }
        }
    }
}

我想知道为什么它会说它 undefined ?=/ 无法解决。它假设在该对象的数组数据中获得位置 [2]。

4

1 回答 1

6

我相信您想要循环sdata[x],而不是sdata在您的内部循环中:

function is_occupied(position) {
    for(var x in sdata){
     for(var y in sdata[x]){
于 2012-04-06T02:22:48.773 回答