Is there any easy way to check whether a deeply nested object is defined? For example, can i do something like
typeof obj.prop.innerprop.yetAnotherInnerProp === 'undefined'
such that it should return true in case any of the inner objects are undefined.
In this case, we know for sure that obj
is defined, but not sure whether prop
, innerprop
or yetAnotherInnerProp
is defined. My function needs the value of yetAnotherInnerProp
, but at runtime the root obj
can have a structure that matches the one shown above, or it could have a different structure like obj.diffprop.diffinner
. I want to check if yetAnotherInnerProp
exists.
I know I could do something like,
if(obj.hasOwnProperty('prop')
if(obj.prop.hasOwnProperty('innerprop')
if(obj.prop.innerprop.hasOwnProperty('yetAnotherInnerProp')
value = obj.prop.innerprop.yetAnotherInnerProp
But I wanted to know if there's an easier way to achieve this.