Arrays 使用具有大量属性而不是数组的json对象

Arrays 使用具有大量属性而不是数组的json对象,arrays,json,properties,Arrays,Json,Properties,而不是数组: var arrayExample = { "lotsOfStuff" : [ {"id" : "th1", "name" : "thing1", "type": "thing", "moo":"a"}, {"id" : "th2", "name" : "thing2", "type": "thing", "moo":"z"}, {"id" : "th3", "name" : "aDifferentThing3", "type": "differentTh

而不是数组:

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;
  ...
}

但是,您可以通过id进行查找,而不必通过索引位置进行查找

使用此阵列替代方案有任何问题吗??
大量元素的性能如何?

如果元素的顺序很重要,请使用数组

如果需要按ID查找,请使用对象

如果需要同时执行这两项操作,请创建一个数组和一个其元素指向相同对象的对象

访问对象属性可能比访问数组元素慢,因为它需要散列而不是简单的索引。但如果需要在数组中按ID查找,则需要进行线性搜索,如果有很多元素,这比散列要慢得多。对象的性能不应受到元素数量的显著影响

for(var id in propertyExample) {
  var thing = lotsOfStuff[id];
  ...
}