I have an object myObj
and a string myStr
. I want to obtain the nested object property from the string variable alone.
How can I accomplish that?
Right now, I'm only getting undefined
. I want 42
.
const myObj = {
foo: {
bar: {
baz: 42
}}};
const myStr = 'foo.bar.baz';
console.log('My answer: ', myObj[myStr],); // desired result: 42