Let's say we have two objects:
parent1.child
parent2.child
If child is undefined in parent1
, I want to use parent2
as parent object (because it'll have the child defined).
Done this way (ES2015):
const conditionalParentObj = (key) => {
parent1[key] === undefined ? parent2[key] : parent1[key];
}
(Or typeof parent1[key] === 'undefined'
, whatever)
But how about nested children?
parent2.child.innerchild
Same logic: If innerchild
is undefined
in parent1
, use parent2
.
But how can I make it work without using a weird conditional logic like passing an innerKey
argument to conditionalParentObj
and return like parent2[innerKey][key]
if innerKey
is passed?