I have a JSON object
{
"data": [{
"user_id":"1",
"user_name":"test",
"user_phone":"2147483647",
"user_email":"test@example.com"
}]
}
and a jQuery loop function
$.each(responseData, function(index, ProfileData) {
profiles.push(
application.getModel("Profile", [ ProfileData.user_id, ProfileData.user_name, ProfileData.user_phone, ProfileData.user_email ] )
);
});
But when the JSON object becomes this
{
"data": [{
"firm_id":"1",
"firm_name":"Firm",
"firm_phone":"2147483647",
"firm_email":"testfirm@example.com"
}]
}
Then I want to check whether the JSON data has user or firm extensions, and also need to use this in the push() instead.
application.getModel( "Profile", [ ProfileData.user_id, ProfileData.user_name, ProfileData.user_phone, ProfileData.user_email ] )
You can check for the existence of a property inside an object like so:
So your solution might look like this:
Another approach would be to coalesce the different properties, and use whichever one is defined: