It is notoriously annoying to get a deeper property from an object. If the parent does not exist, you get an error. So you need to stop getting to the property when a parent doesn't exist.
The clueless way to do this is:
if (parent.value && parent.value.subvalue && parent.value.subvalue.targetvalue)
var myVal = parent.value.subvalue.targetvalue;
else
var myVal = null;
When you need to get a property often, this is unworkable and you need a shortcut function. First I did something like this:
function getProp(path) {
try {
return eval(path);
}
catch (e) {
return null;
}
};
// getProp('parent.value.subvalue.targetvalue');
But this is lame for at least two reasons: The object must be in the scope, and no one likes to use eval()
.
So maybe it's better to apply the object to the function:
function getProp(path, parent) {
path = path.split('.');
var val = parent;
for (var k = 0; k < path.length; k++) {
var key = path[k];
try {
val = val[key];
}
catch (e) {
return null;
}
}
return val;
};
// getProp('value.subvalue.targetvalue', parent);
But somehow it still doesn't feel right.
How do you do this? What is best practice?
Setting an objects deep property of which the parents may or may not exist is even more annoying.
parent = parent || {};
parent.value = parent.value || {};
parent.value.subvalue = parent.value.subvalue || {};
parent.value.subvalue.target = "YAY SOME VALUE!"
How would you tackle this nicely?
Are there javascript native functions for this yet, since this needs to be done often?