Search code examples
javascriptlodash

How to deep pick in another pick with lodash?


Now i have code like this:

var object = {
   a: 'a',
   b: 'b',
   c: {
       d: 'd'
   }
}
_.get(object).pick(['a', 'b']).value();

How to deep pick property 'd' like:

_.get(object).pick(['a', 'b', 'c.d']).value();

Solution

  • In case you insist in using Lodash, consider using the _.get() function:

    _.get(object, 'c.d');
    

    So, for the properties you want to get:

    const selectedProps = {
      ..._.pick(object, ['a', 'b']),
      _.get(object, 'c.d')
    }