0

Consider the following JSON string:

[{"ElementID1":{"latitude":"10.02483","longitude":"70.753464"}},{"ElementID2":{"latitude":"10.029301","longitude":"70.751892"}},{"ElementID3":{"latitude":"10.029568","longitude":"70.751856"}}]

Which is contained in the "data" variable:

var response = JSON.parse(data);

How do I go through this result? It is clear for me that I can access this first as an array:

for(var element in response)
{

}

But I don't know what "ElementID1" will be. It can be any string so i cant just do something like

element.elementID.latitude 

To retrieve the object latitude. And i would also like to be able to get that picture id itself.

I think this is a simple question but i have tried googling for the answer for a while without any progress.

Pochi
  • 13,391
  • 3
  • 64
  • 104
  • You shouldn't use `for..in` to iterate over an array, use the vanilla `for`. I'm not sure I understand completely what you are asking, but the answer probably is, well, `for..in`. Use this loop in any object and it will loop over its keys just as it is looping over the array's keys. In other words: is this what you want?: `for(var i=0, n=response.length; i – acdcjunior Jun 19 '14 at 03:05
  • I have to agree with @acdcjunior: `for...in` loops in JS don't actually give you the element, just the index. [Demo](http://jsfiddle.net/AstroCB/Np6qR/) – AstroCB Jun 19 '14 at 03:22

3 Answers3

3
var data = [{"ElementID1":{"latitude":"10.02483","longitude":"70.753464"}},
            {"ElementID2":{"latitude":"10.029301","longitude":"70.751892"}},
            {"ElementID3":{"latitude":"10.029568","longitude":"70.751856"}}];

for(var i = 0; i < data.length; i++) {
    var obj = data[i];    
    for (var key in obj) {
       if (obj.hasOwnProperty(key)) {
          console.log(obj[key].latitude);
       }
    }
}

DEMO

Basically, you need to access each data array element, which is an object in this case, and since you don't know the name of ElementID* for each element, you can loop through the properties of this object looking for the desired one, latitude in this case.

Diana Nassar
  • 2,303
  • 14
  • 17
  • A bit of explanation along with your code, while obviously not necessary, often helps the OP understand your answer. – AstroCB Jun 19 '14 at 03:19
  • Thank you, this works perfectly. I had actually tried this but with a "for..in" at first. I wonder why the behavior of the loops are different. – Pochi Jun 19 '14 at 04:08
  • You're most welcome :) actually, it does work with "for..in" this way: `for(var item in data) { var obj = data[item]; for (var key in obj) { if (obj.hasOwnProperty(key)) { console.log(obj[key].latitude); } } }` – Diana Nassar Jun 19 '14 at 04:20
  • You could, but [you shouldn't use `for..in` with arrays](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea). – acdcjunior Jun 19 '14 at 06:11
0

You can do the following:

response.forEach(function(response){
  for (var element in response){
    var picutureId = element;
    var latitude = value[element].latitude;
    var longitude = value[element].longitude; 
   // at this point you can manipulate the pictureId, latitude, and longitude of each object
  }
})

Loop through the results array, loop through each object's key/val pair and manipulate the data from there.

JoshSGman
  • 529
  • 2
  • 6
  • 16
0

If even latitude and longitude attribute names are unknown

for (var index in data) { //Iterate through array
    var obj = data[index];
    for (var prop in obj) { //Iterate through ElementId object
        console.log(prop); //Print ElementID
        var latlongObj = obj[prop];
        for (var key in latlongObj) { //Iterate through latlong object
            console.log(key); //Print lattitude/longitude
            console.log(latlongObj[key]); //Print value
        }
    }
}
Okky
  • 10,338
  • 15
  • 75
  • 122