JSFiddle - React, Tailwind, and code Playground
JavaScript
const a = {
b: {
c:{
d: {}
}
}
};
function nestIfUndef(obj, props = []) {
if (props.length === 0) {
return obj;
} else {
if ( obj[props[0]] === undefined ){
obj[props[0]] = {};
}
return nestIfUndef(obj[props[0]], props.slice(1));
}
}
nestIfUndef(a, ['b', 'c', 'd', 'e','f','g']);
nestIfUndef(a, ['b', 'c', 'x', 'y', 'z']);
console.log(a);