【发布时间】:2014-11-28 02:57:14
【问题描述】:
而不是数组:
var arrayExample = {
"lotsOfStuff" : [
{"id" : "th1", "name" : "thing1", "type": "thing", "moo":"a"},
{"id" : "th2", "name" : "thing2", "type": "thing", "moo":"z"},
{"id" : "th3", "name" : "aDifferentThing3", "type": "differentThing", "moo":"m"}
]
}
使用很多属性:
var propertyExample = {
"lotsOfStuff" : {
"id1" : {"name" : "thing1", "type" : "thing", "moo" : "a" },
"id2" : {"name" : "thing2", "type" : "thing", "moo" : "z" },
"id3" : {"name" : "aDifferentThing3", "type" : "differentThing", "moo" : "m" }
}
}
仍然可以遍历它们
for(var idx in arrayExample.lotsOfStuff) {
var thing = lotsOfStuff[idx];
var id = thing.id;
...
}
和
for(var id in propertyExample) {
var thing = lotsOfStuff[id];
...
}
但是您可以通过 id 进行查找,但会以索引位置查找为代价
使用这个数组替代品有什么问题吗? 有很多元素的性能怎么样??
【问题讨论】:
标签: arrays json properties