JSFiddle - React, Tailwind, and code Playground

JavaScript

const a = {
 b: {
  c:{
    d: 2
  }
 }
};

function nestIfUndef(obj, props = []) {
  if (props.length === 0) {
    return obj;
  } else {

    if ( obj[props[0]] === undefined ){
        console.log("before: "+obj[props[0]]);
        obj[props[0]] = {};
        console.log("after: "+obj[props[0]]);
    }

    return nestIfUndef(obj[props[0]],  props.slice(1));
  }
}

console.log(nestIfUndef(a, ['b', 'c', 'd', 'e','f','g']));