/// see for more examples https://stackoverflow.com/questions/8817394/javascript-get-deep-value-from-object-by-passing-path-to-it-as-string
const config = {
a:'a',
b:'b',
c:{
s:'r',
t:{
s:'terst'
}
}
};
var obj = {
foo: { bar: 'baz' }
};
const getValue = (obj, item) =>{
items=item.split('.')
for (var i=0, len=items.length; i<len; i++){
obj = obj[items[i]];
}
return obj;
};
//console.log(getValue(config, 'c.s'));
function test(obj, path) {
path.split('.').forEach(prop => {obj = obj[prop]});
return obj
}
// below one is the best option
const lens = (obj, path) => path.split(".").reduce((o, key) => o && o[key] ? o[key] : undefined, obj);
console.log(lens(config, 'c.t'))
//console.log(test(config, 'c.t.s'))