I have an array of objects like this:
var characters = [
{ 'name': 'barney', 'age': 36, 'salary':{'amount': 10} },
{ 'name': 'fred', 'age': 40, 'salary':{'amount': 20} },
{ 'name': 'pebbles', 'age': 1, 'salary':{'amount': 30} }
];
I want to get the salary amounts into an array. I managed to do it by chaining two pluck
functions, like this:
var salaries = _(characters)
.pluck('salary')
.pluck('amount')
.value();
console.log(salaries); //[10, 20, 30]
Is there a way to do this by using only one pluck
? Is there a better way with some other function in lodash?
You can just give the path to be used as a string, like this
console.log(_(characters).pluck('salary.amount').value())
// [ 10, 20, 30 ]
Or use it directly
console.log(_.pluck(characters, 'salary.amount'));
// [ 10, 20, 30 ]
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