I have a json object r:
[
{
"id" : "3443",
"name" : "Joe",
"date1" : "254",
"date4" : "261"
},
{ "id" : "50942",
"name" : "John",
"date2" : "192",
"date4" : "195"
},
{ "id" : "2524",
"name" : "Mary",
"date1" : "153",
"date4" : "163"
}
]
and I wish to have a Javascript For loop to go through the dateX values.
I know that X is between 1 and a Max value.
But following code does not work:
for (j=1; j<=Max; j=j+1) {
datestring = 'date' + j;
if (isset(r[i].datestring)) value[j] = r[i].datestring;
else value[j] = null;
}
Forgot to explain that I defined the isset function as:
function isset(variable) {
return typeof(variable) != "undefined" && variable !== null;
}
First iterate through the array of objects with an ordinary for loop. Then iterate through the properties of each object with
for (var p in obj)and check the value ofp. Something like:It’s counter-intuitive and inefficient to loop through all possible property values to check their existence. Instead, loop through the actual properties and test their names against your pattern.