Javascript 相当于 _.pick() 但对于 Lo-dash 中的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30464259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
equivalent of _.pick() but for array in Lo-dash
提问by pery mimon
I have the following collection:
我有以下收藏:
var columns = [
{ key:'url', width:20, type:'text' },
{ key:'title', width:21, type:'text' },
{ key:'desc', width:22, type:'text' },
{ key:'domain', width:23, type:'text' },
{ key:'user', width:24, type:'text' }
];
I'm looking for a method to map an array of objects with picked keys, something along the lines of:
我正在寻找一种方法来映射具有所选键的对象数组,类似于:
_.mapPick(columns, [width])
// [{width:20},{width:21},{width:22},{width:23},{width:24}]
I know I can extend lo-dash like this:
我知道我可以像这样扩展 lo-dash:
_.mixin({
mapPick: mapPick:function (objs,keys){
return _.map(objs, function (obj) {
return _.pick(obj,keys)
})
}
});
I'm not sure if there is some native function that I'm missing.
我不确定是否有一些我缺少的本机功能。
I found a similar question herebut I'm looking for a more lo-dash native way.
回答by Adam Boduch
回答by Vlad
In my case I also need cast to type, and problem was what TS compiler represents Adam Boduch solution return a boolean, so I find a little simple way to do this:
在我的情况下,我还需要强制类型转换,问题是 TS 编译器代表 Adam Boduch 解决方案返回一个布尔值,所以我找到了一个简单的方法来做到这一点:
_.map(columns, item => { return { key: item.key } });
In this case you also can add some new properties.
在这种情况下,您还可以添加一些新属性。
P.S. Because I cant post a comment I will add explanation about partialRight usage here:
PS 因为我不能发表评论,所以我将在此处添加有关 partialRight 用法的解释:
_.map(columns, _.partialRight(_.pick, 'key'));
Firstif u call _.map(array, func)funcwill be called for every array element. So it's equal: _.map(array, item => func(item)).
首先,如果您调用_.map(array, func)func将针对每个数组元素调用。所以它是相等的:_.map(array, item => func(item)).
Secondthe result of call partialRightwill be a function newFunc, so we can represent Adam's code as:
其次,调用partialRight的结果将是一个函数newFunc,因此我们可以将 Adam 的代码表示为:
var newFunc = _.partialRight(_.pick, 'key');
_.map(array, item => newFunc(item));
Thirdwe can represent the newFunclogic as:
第三,我们可以将newFunc逻辑表示为:
function newFunc(item) {
return _.pick(item, 'key')
}
FinallyI think most understandable and readable solution for this problem is:
最后,我认为这个问题最容易理解和可读的解决方案是:
_.map(columns, item => _.pick(item, 'key'))
回答by David
I found simpler solution in map examples:
我在地图示例中找到了更简单的解决方案:
var users = [
{name:'David',age:35},
{name:'Kate',age:28},
];
_.map(users,'age'); // [35,28]
回答by alexloehr
With ES6 destructuringand arrow functionsyou can do it like this:
columns.map(({width}) => ({width}))
No lodash required.
不需要 lodash。
回答by Metalstorm
If you only want one of the keys you can do:
如果您只想要其中一个键,您可以执行以下操作:
_.map(collection, 'key')
Your example:
你的例子:
var columns = [
{ key:'url', width:20, type:'text' },
{ key:'title', width:21, type:'text' },
{ key:'desc', width:22, type:'text' },
{ key:'domain', width:23, type:'text' },
{ key:'user', width:24, type:'text' }
];
var widths = _.map(columns, 'width');
widths
[20, 21, 22, 23, 24]

