javascript - Using Lodash `_.get` to access object key using bracket notation -


i have following

const key = 'foo'; const test = { foo: { bar: 23 } }; 

and i'd use lodash get access value of test[key].bar.

i want use bracket notation on first indicator...

_.get(test, '[key].bar'); // results in undefined 

surely there's way...

you need put value of key path string:

_.get(test, key + '.bar'); 

in es2015 can use template literal (interpolated string):

_.get(test, `${key}.bar`); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -