admin管理员组文章数量:1290237
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?
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?
1 Answer
Reset to default 7You 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 ]
本文标签: javascriptLodash get nested object property with pluckStack Overflow
版权声明:本文标题:javascript - Lodash get nested object property with pluck - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741492606a2381692.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论