如何在javascript上获取对象的所有值
我有这样的目标:如何在javascript上获取对象的所有值,javascript,for-loop,Javascript,For Loop,我有这样的目标: [ { "pk": 2, "model": "gps.positions", "fields": { "power": null, "altitude": null, "time": "2014-04-08T04:00:00Z", "longitude": 47.916142, "course": null, "other": "", "device": 11,
[
{
"pk": 2,
"model": "gps.positions",
"fields": {
"power": null,
"altitude": null,
"time": "2014-04-08T04:00:00Z",
"longitude": 47.916142,
"course": null,
"other": "",
"device": 11,
"address": "test",
"latitude": 29.30661,
"speed": null,
"valid": null
}
},
{
"pk": 3,
"model": "gps.positions",
"fields":{
"power": null,
"altitude": null,
"time": "2014-04-08T20:00:00Z",
"longitude": 47.91,
"course": null,
"other": "",
"device": 11,
"address": "test2",
"latitude": 29.30661,
"speed": null,
"valid": null
}
}
]
pk fields.power etc.
2 null
3 null
for(var i=0; i<data.length, i++){
data[i].pk //returns only one value i.e 3
}
我希望得到如下值:
[
{
"pk": 2,
"model": "gps.positions",
"fields": {
"power": null,
"altitude": null,
"time": "2014-04-08T04:00:00Z",
"longitude": 47.916142,
"course": null,
"other": "",
"device": 11,
"address": "test",
"latitude": 29.30661,
"speed": null,
"valid": null
}
},
{
"pk": 3,
"model": "gps.positions",
"fields":{
"power": null,
"altitude": null,
"time": "2014-04-08T20:00:00Z",
"longitude": 47.91,
"course": null,
"other": "",
"device": 11,
"address": "test2",
"latitude": 29.30661,
"speed": null,
"valid": null
}
}
]
pk fields.power etc.
2 null
3 null
for(var i=0; i<data.length, i++){
data[i].pk //returns only one value i.e 3
}
pk字段、电源等。
2空
3空
对于(变量i=0;ivar o=
变量i,l;
对于(i=0,l=o.length;i
您到底想要什么?您已经在数据[i]中获得了价值
这是一个对象。正如你所看到的pk
有两个值2和3
,我只得到3
来解释你想要实现的目标,这并不清楚当你的I==1
时,首先循环,然后你得到2
作为一个值。我看到你的代码在工作,我得到了这两个值“2”和“3”