I'm sure it's somewhere inside the LoDash docs, but I can't seem to find the right combination.
var users = [{
id: 12,
name: 'Adam'
},{
id: 14,
name: 'Bob'
},{
id: 16,
name: 'Charlie'
},{
id: 18,
name: 'David'
}
]
// how do I get [12, 14, 16, 18]
var userIds = _.map(users, _.pick('id'));
Since version v4.x you should use _.map
:
_.map(users, 'id'); // [12, 14, 16, 18]
this way it is corresponds to native Array.prototype.map method where you would write (ES2015 syntax):
users.map(user => user.id); // [12, 14, 16, 18]
Before v4.x you could use _.pluck
the same way:
_.pluck(users, 'id'); // [12, 14, 16, 18]
With pure JS:
var userIds = users.map( function(obj) { return obj.id; } );
In the new lodash release v4.0.0 _.pluck
has removed in favor of _.map
Then you can use this:
_.map(users, 'id'); // [12, 14, 16, 18]
You can see in Github Changelog