LoDash: Get an array of values from an array of object properties
Solution 1:
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]
Solution 2:
With pure JS:
var userIds = users.map( function(obj) { return obj.id; } );
Solution 3:
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
Solution 4:
And if you need to extract several properties from each object, then
let newArr = _.map(arr, o => _.pick(o, ['name', 'surname', 'rate']));