如何获得代码片段的以下输出?

how to get the following output of the code snippet?

我在面试中被问到这个问题。如何解决这个问题?提到了对象和控制台语句。我不知道如何实现函数 findPath?

这个可以做到

var obj = {
  a: {
    b: {
      c: 1
    }
  }
}
function findPath(path) {
  const paths = path.split('.');
  let innerObj = {...obj};
  for (let i = 0; i < paths.length; i++) {
    innerObj = innerObj && innerObj[paths[i]] || null;
  }
  return innerObj;
}

console.log(findPath("a.b.c"));
console.log(findPath("a.b"));
console.log(findPath("a.b.d"));
console.log(findPath("a.c"));
console.log(findPath("a.b.c.d"));
console.log(findPath("a.b.c.d.e"));

var obj = {
  a: {
    b: {
      c: 1
    }
  }
}

obj.findPath = function(path) {
  const keys = path.split('.');
  return keys.reduce((currentPath, key) => {
    return currentPath && currentPath[key]
  }, this) 
}

console.log(obj.findPath('a'))
console.log(obj.findPath('a.b'))
console.log(obj.findPath('a.b.c'))
console.log(obj.findPath('a.b.c.d'))

class Obj {
  constructor() {
    this.data = {
      a: {
        b: {
          c: 12
        }
      }
    };
  }

  findPath = (str) => {
    let sol = this.data;
    for (let key of str.split(".")) {
      sol = sol[key];
      if (!sol) {
        return undefined;
      }
    }
    return JSON.stringify(sol);
  };
}

let obj = new Obj();
console.log(obj.findPath("a.b.c"));
console.log(obj.findPath("a.b"));
console.log(obj.findPath("a.b.d"));
console.log(obj.findPath("a.c"));
console.log(obj.findPath("a.b.c.d"));
console.log(obj.findPath("a.b.c.d.e"));