0

我有一个这样的对象列表(从 PHP 创建然后 json 编码):

{"10":{"10":["4","7","2","2","0","32"],"11":["5","22","1","1","0","0"]}}

translated:

{"X":{"Y":["unique_id","data","data","data","data","data"],"Y":["unique_id","data","data","data","data","data"]

但是我试图找到一种方法来通过寻找 unique_id 来获得 X:Y 让我们说它是 4。

最有效的方法是什么?

4

1 回答 1

1

如果它们是唯一 ID,则创建一个单独的查找表。

var id_lookup = {};

for(var x in data)
    for(var y in data[x])
        id_lookup[data[x][y][0]] = [x,y]

var coords = id_lookup[4];

alert(coords);  // [10,10]

alert(data[coords[0]][coords[1]]); // ["4","7","2","2","0","32"]

演示:http: //jsfiddle.net/LXE3B/

于 2012-04-13T22:37:17.800 回答