How do I iterate through this JSON object in jQuery?

95,863

Solution 1

Since you tagged your question as a jquery one, you should use $.each because it's jquery's iterator function:

$.each(data.dates, function(index, element) {
    alert(element.timeStamp); 
});

If you want to stick to the for in syntax (which i see you've tried), a solution might be :

for(var key in data.dates) {
     alert(data.dates[key].timeStamp); 
} 

But beware that the for in syntax may do more than you think it does: it iterates over the properties inherited from the prototype too, so it might be usefull to make sure you iterate only on the object instance properties:

for(var key in data.dates) {
    // if it's not something from the prototype
    if(data.dates.hasOwnProperty(key)) {
        alert(data.dates[key].timeStamp); 
    }
} 

update
Another elegant way is to use the Object.keys method that returns an array containing all the keys in the targeted object to iterate over all the object's properties:

for(var i=0, keys=Object.keys(data.dates), l=keys.length; i<l; i++) {
    alert(data.dates[i].timeStamp);
} 

Solution 2

You use $.each().
It looks like this:

$.each(data, function(n, elem) {
    // here you process your data to data loaded to lines               
});

Solution 3

You can simply iterate through the json structure using jQuery each:

$.each(data, function(index, element) {
   alert(element.dates.timeStamp); 
});
Share:
95,863

Related videos on Youtube

Johnathan Au
Author by

Johnathan Au

Updated on July 09, 2022

Comments

  • Johnathan Au
    Johnathan Au almost 2 years

    I have a JSON object which is generated by PHP. It's an object with a set of dates. It has the timeStamp and then a formatted version of the date. How would I iterate through this in jQuery?

    {
      "dates":[
        {
          "timeStamp": 1317596400,
          "formattedDate": "Mon 03 October 2011"
        },
        {
          "timeStamp": 1317682800,
          "formattedDate": "Tue 04 October 2011"
        },
        {
          "timeStamp": 1317855600,
          "formattedDate": "Thu 06 October 2011"
        }
      ]
    }
    

    I've tried:

    for (var i in data) { 
      alert(data.dates[i].timeStamp); 
    };
    
    for (var i in data) { 
      alert(data[i].dates.timeStamp); 
    };
    

    and

    for (var i in data) { 
      alert(data.dates.timeStamp[i]); 
    };
    
    • Mike Kormendy
      Mike Kormendy almost 8 years
      I cleaned up your code for posterity, this is a good example.
  • Johnathan Au
    Johnathan Au about 12 years
    Hey thanks, I decided to stick with the for in syntax. Just something I'm more familiar with coming from a PHP and Java background
  • gcoleman0828
    gcoleman0828 about 10 years
    $.each does not work in situations where there is no length and will just be passed over.
  • Mike Simmons
    Mike Simmons about 10 years
    the OP is attempting to iterate through an array of dates, arrays have a length: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • KuroNeko
    KuroNeko almost 6 years
    Shouldn't it be "i" instead of "key" in the alert?..Using 'i' worked for me
  • gion_13
    gion_13 almost 6 years
    Thanks for noticing, @Ravin! It was a typo - the power of copy-paste :)