function findInObjByDottedPath(path, obj) {
const pathElements = path.split('.');
const element = obj[pathElements[0]];
if (element) {
if (typeof(element) === "object" && !Array.isArray(element)) {
return findInObjByDottedPath(
pathElements.slice(1).join('.'),
element
);
}
return element;
}
return null;
}
const myObj = {
foo: {
bar: {
baz: 42
}
}
};
console.log(findInObjByDottedPath("foo.bar.baz", myObj));
console.log(findInObjByDottedPath("foo.bar.not.a.path.should.be.null", myObj));