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.

2
  • 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<n; i++) { for(var key in response[i]) { console.log(key); } }
    – acdcjunior
    Jun 19, 2014 at 3:05
  • I have to agree with @acdcjunior: for...in loops in JS don't actually give you the element, just the index. Demo
    – AstroCB
    Jun 19, 2014 at 3:22

3 Answers 3

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.

4
  • A bit of explanation along with your code, while obviously not necessary, often helps the OP understand your answer.
    – AstroCB
    Jun 19, 2014 at 3: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, 2014 at 4: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); } } } Jun 19, 2014 at 4:20
  • You could, but you shouldn't use for..in with arrays.
    – acdcjunior
    Jun 19, 2014 at 6: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.

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
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.