LoDash:从对象属性数组中获取值数组 [英] LoDash: Get an array of values from an array of object properties
本文介绍了LoDash:从对象属性数组中获取值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我确定它在LoDash文档中,但我似乎找不到正确的组合.
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]
这样,它对应于本机 Array .prototype.map 方法(ES2015语法):
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]
在v4.x之前,您可以使用 _.pluck
相同的方式:
Before v4.x you could use _.pluck
the same way:
_.pluck(users, 'id'); // [12, 14, 16, 18]
这篇关于LoDash:从对象属性数组中获取值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文