A api returns a JSON response at undefinable depth where some elements may or not be included. How do I check if a property is present? Some of the parameters include weird names like "@param" or "$". Example for this follows.
I wrote a check for something similar, but cannot use the dot syntax for above reasons. Help for modifiying this is highly appreciated.
function checkValue(objectPath) {
var keys = Object.isArray(objectPath) ? objectPath : objectPath.split(".");
if (keys[0] == "window") keys.shift();
try {
return keys.inject(window, function(obj, key) {return obj[key];});
} catch (e) {
return undefined;
}
}
Example for the object to test on is:
var obj = {
member: {
'@member-age': {
value: 42
}
}};
Before I checked it with checkValue(obj.member.age) but cannot do this for obvious reasons in this example.