您将列表定义为object
. 对象属性按它们定义的顺序排列,但我们不能相信它的顺序。我认为有关于它的 Chrome 错误https://code.google.com/p/v8/issues/detail?id=164
以下first
方法给出列表对象的第一个元素,此函数在大多数情况下都有效,如果对象为空,它将返回undefined
值。
var data = {
"_id":"123",
"list":{
"56":{"name":"Great ","amount":100,"place":"Town"},
"57":{"name":"Great 2","amount":200,"place":"City"}
},
"pop":[2,3]
};
function first( data ){
for ( var i in data ){
if ( data.hasOwnProperty(i) ) break;
}
return data.hasOwnProperty(i) ? data[i] : undefined;
}
first( data.list ).amount;
如果你想保持列表的顺序 .. 将它们定义为Array
. 例子
var data = {
"_id":"123",
"list":[
{"id" : "56" ,"name":"Great ","amount":100,"place":"Town"},
{"id" : "57" ,"name":"Great 2","amount":200,"place":"City"}
],
"pop":[2,3]
};
并以data.list[0]