doing the following is risking an exception 'there is no Bar on undefined'
var obj = o.Foo.Bar
the only way i can think of doing above safely :
var obj = (o && o.Foo && o.Foo.Bar ) ? o.Foo.Bar : null;
or putting the entire thing in try/catch which is not an option in most cases for me.. and results in more code if i want a different thing to happen depending on which property is missing.
is there a good concise way to perform this assignment safely?
** update **
tweaked @techfoobar's answer
function resolve(obj, propertyPath) {
if (!propertyPath) return;
var props = propertyPath.split('.');
var o = obj;
for(var i in props) {
o = o[props[i]];
if(!o) return false;
}
return o;
}
var res = resolve(obj, 'ApiResponse.CommandResponse');
if (res){
for (var i in res){
seems like as good as it's going to get...