Get nested property from an Object:
f(null, _) = null
f(a, []) = a
f(a, x:xs) = f(a[x], xs)
function getProp(obj, propStr) {
const arr = propStr.split(".");
function get(obj, propArr) {
if (obj == null) {
return null;
} else if (propArr.length === 0) {
return obj;
} else {
const head = propArr[0];
const tail = propArr.slice(1);
return get(obj[head], tail);
}
}
return get(obj, arr);
}
// Usage:
const obj = {
a: {
b: 1
}
};
console.log(getProp(obj, "a.b"));
Get nested property from an Object: