Lodash for "select by object path"?

Let's say I have this object (or an array of these objects):

var person = { birth: { place: { country: 'USA' } } }; 

I thought there was a lodash function where I could go through 'birth.place.country' and return the value USA .

Is there such a function in lodasdh 3.x or am I Iming This?

+6
source share
2 answers

You can use the _.get function:

 _.get(person, 'birth.place.country', 'optionalDefaultValue'); 

lodash also provides a function called _.result , which can also call functions.

+13
source

Note: for an array of these objects

_.map(people, 'birth.place.country')

provides the same functionality as undefined answer

+2
source

All Articles