vendredi 2 janvier 2015

What is the best way to iterate properties inside a JavaScript Object?


I have the following javaScript object:



var theData = {
"data": [
{
"name": "Event1",
"start_time": "2015-01-15T22:00:00-0200",
"end_time": "2015-01-16T07:00:00-0200",
"timezone": "America/Sao_Paulo",
"location": "Somewhere",
"id": "824583590897935"
},
{
"name": "Event2",
"start_time": "2015-01-14T22:00:00-0200",
"timezone": "America/Sao_Paulo",
"location": "Somewhere",
"id": "1522505051359796"
}
],
"paging": {
"cursors": {
"after": "MzA0MjY5OTI5NzgwODIz",
"before": "ODI0NTgzNTkwODk3OTM1"
},
"next": "http://ift.tt/1BdlMmR"
}
}


So i want to iterate over this object. I have the following code that is working. But I'm new to Javascrip and this way i'm doing is a high complexity way. How could i do it as simple as possible once this will be called for a huge amount of times?



for (var key in theData) {
var obj = theData[key];
for (var prop in obj) {
var eventObj = obj[prop];
for (var attribute in eventObj){
if(eventObj.hasOwnProperty(attribute)){
console.log(attribute + " = " + eventObj[attribute]);
}
}
}
}




Aucun commentaire:

Enregistrer un commentaire