equivalent of _.pick() but for array in Lo-dash equivalent of _.pick() but for array in Lo-dash arrays arrays

equivalent of _.pick() but for array in Lo-dash


I think the map() + pick() approach is your best bet. You could compose the callback instead of creating an inline function however:

_.map(columns, _.partialRight(_.pick, 'key'));


In my case I also need cast to type, and problem was what TS compiler represents Adam Boduch solution return a boolean, so I find a little simple way to do this:

_.map(columns, item => { return { key: item.key } });

In this case you also can add some new properties.

P.S. Because I cant post a comment I will add explanation about partialRight usage here:

_.map(columns, _.partialRight(_.pick, 'key'));

First if u call _.map(array, func) func will be called for every array element. So it's equal: _.map(array, item => func(item)).

Second the result of call partialRight will be a function newFunc, so we can represent Adam's code as:

var newFunc = _.partialRight(_.pick, 'key');_.map(array, item => newFunc(item));

Third we can represent the newFunc logic as:

function newFunc(item) {    return _.pick(item, 'key')}

Finally I think most understandable and readable solution for this problem is:

_.map(columns, item => _.pick(item, 'key'))


I found simpler solution in map examples:

var users = [    {name:'David',age:35},    {name:'Kate',age:28},];_.map(users,'age'); // [35,28]