Damian Hetman Damian Hetman - 17 days ago 10
Javascript Question

Get inside Javascript object in "for" loop

I need to get lat and lng inside list. I have something like this and don't know how to get these values.

var list = {
"punkt1": {
"lat": 49.834390,
"lng": 19.068899
},
"punkt2": {
"lat": 49.834990,
"lng": 19.068199
}
};

var count = Object.keys(list).length;

for (var i=0; i<count; i++) {
// Get inside list[i] -> lat,lng
// If statement
}

Answer

You can do it a few ways, here are two:

  var list = {
    "punkt1": {
    "lat": 49.834390,
    "lng": 19.068899
    },
    "punkt2": {
    "lat": 49.834990,
    "lng": 19.068199
    }
  };

  var count = Object.keys(list).length;

  for (var i=0; i<count; i++) {
    var currObj = list[Object.keys(list)[i]];
    console.dir(currObj);
  }

for (item in list){
  console.log(item);
  console.dir(list[item]);
}