Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I iterate through this JSON object in jQuery?

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]);  }; 
like image 555
Johnathan Au Avatar asked Mar 27 '12 09:03

Johnathan Au


People also ask

How do I iterate through a JSON object?

Use Object.values() or Object. entries(). These will return an array which we can then iterate over. Note that the const [key, value] = entry; syntax is an example of array destructuring that was introduced to the language in ES2015.

How do I iterate over an object in jQuery?

each(), which is used to iterate, exclusively, over a jQuery object. The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

Can I use loop in JSON?

To loop through a JSON array with JavaScript, we can use a for of loop. to loop through the json array with a for of loop. We assign the entry being looped through to obj . Then we get the value of the id property of the object in the loop and log it.


2 Answers

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); }  
like image 200
gion_13 Avatar answered Sep 24 '22 18:09

gion_13


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

$.each(data, function(n, elem) {     // here you process your data to data loaded to lines                }); 
like image 24
mfadel Avatar answered Sep 21 '22 18:09

mfadel