简体   繁体   中英

how to get all the values of objects on javascript

I have object like this:

[
  {
    "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
    }
  }
]

I want to get values like this:

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
}
var o = <your object>

var i, l;
for (i = 0, l = o.length; i < l; i += 1) {
  console.log(o[i].pk, o[i].fields.power);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM