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
And if you need to extract several properties from each object, then
let newArr = _.map(arr, o => _.pick(o, ['name', 'surname', 'rate']));
Simple and even faster way to get it via ES6
let newArray = users.flatMap(i => i.ID) // -> [ 12, 13, 14, 15 ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With