什么's是相当于LINQ's SelectMany运算符
What's the underscore.js equivalent to LINQ's SelectMany operator?
想象一下我有一个嵌套的数组结构。
var nested = [ [1], [2], [3] ];
使用underscore.js,我将如何生成一个扁平的数组?
在C#中,您可以像这样使用Enumerable.SelectMany
:
var flattened = nested.SelectMany(item => item);
请注意,本例中的lambda直接选择嵌套项,但它可以是任意表达式。
在jQuery中,可以只使用:
var flattened = $.map(nested, function(item) { return item; });
然而,这种方法不适用于下划线的映射函数。
那么,如何使用underscore.js获得扁平数组[1, 2, 3]
呢?
如果你有一个稍微复杂一点的数组,比如来自JSON的数组,你也可以利用pulture方法,提取你感兴趣的特定属性,类似于parents.SelectMany(parent => parent.Items);
// underscore version
var allitems = _.flatten(_.pluck(parents, 'items'));
allitems
现在是来自父项[a,b,c,d]
的所有子项的数组。
还有一个JSFiddle展示了同样的东西。
或者,如果你正在使用lodash,你可以通过使用_.flatMap函数来做同样的事情,该函数从版本4开始就可用了。感谢诺埃尔在评论中指出了这一点。
var parents = [
{ name: 'hello', items: ['a', 'b'] },
{ name: 'world', items: ['c', 'd'] }
];
// version 1 of lodash, straight up
var allitems = _.flatMap(parents, 'items');
logIt('straight', allitems);
// or by wrapping the collection first
var allitems = _(parents)
.flatMap('items')
.value();
logIt('wrapped', allitems);
// this basically does _(parents).map('items').flatten().value();
function logIt(wat, value) {
console.log(wat, value)
}
<script src="https://cdn.jsdelivr.net/lodash/4.16.6/lodash.min.js"></script>
<pre id="result"></pre>
如果你想做更多的事情,又不想连锁运算符,你可以使用flow
函数来获得同样的效果。如果您使用TypeScript并单独导入每个运算符,这将非常有用,因为您可以优化最终负载。
const parents = [
{ name: 'hello', items: ['a', 'b'] },
{ name: 'world', items: ['c', 'd'] }
];
logIt('original', parents);
const result = _.flow(
(collection) => _.flatMap(collection, (item) => item.items),
(flattened) => flattened.filter((item) => item !== 'd')
)(parents);
logIt('result without "d"', result);
function logIt(wat, value) {
console.log(wat, value);
}
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
<pre id="result"></pre>
var nested = [ [1], [2], [3] ];
var flattened = _.flatten(nested);
这是一个小提琴
我们还可以将Patrick的解决方案制作成一个mixin,使其成为可链接的:
_.mixin({
selectMany: function(collection, iteratee=_.identity) {
return _.flatten(_.map(collection, iteratee));
}
});
示例:
let sample = [{a:[1,2],b:'x'},{a:[3,4],b:'y'}];
console.log(_.selectMany(sample, 'a')); // [ 1, 2, 3, 4 ]
console.log(_.chain(sample).selectMany(o => o.a).filter(a => a % 2 === 0).map(a => a * 3).value()); // [ 6, 12 ]
我在lodash中找不到任何类似SelectMany
的方法,所以我使用纯JS:创建了一个
Array.prototype.selectMany = function(fn) {
return Array.prototype.concat(...this.map(fn));
};
轰。
> console.log([{a:[1,2],b:'x'},{a:[3,4],b:'y'}].selectMany(o => o.a));
[ 1, 2, 3, 4 ]
相关文章:
- 这是什么 ==- javascript 运算符
- jQuery/JS包含运算符或类似运算符
- Javascript逻辑运算符和字符串/数字
- 模运算符前的双水平线
- jQuery's trim()的前缀为not运算符
- Javascript,如果条件在没有&&逻辑运算符当&&它不起作用
- 如果在构造函数中有“返回”,则在 JavaScript 中的新运算符中做了什么
- 如何使用排列运算符来获取具有命名参数的函数的所有参数
- JavaScript 中三元条件和逻辑和运算符的运算符优先级
- 用于验证JS中逻辑运算符表达式的正则表达式
- 什么's是相当于LINQ's SelectMany运算符
- JavaScript-===vs===运算符性能
- 在 Jquery/Javascript 中使用多个 OR (||) 运算符时如何设置变量
- 三元运算符检查多个字符串
- 调用C++中JScript中的instanceof运算符(IDispatch/IDispatchEx)
- ||(OR)运算符如何在赋值中工作
- 在 JavaScript 对象中设置要使用的运算符的属性
- 哪个JavaScript相等运算符(==或===)更快
- 逻辑和/或运算符-意外的最终结果
- 扩展Nashorn-ecma引擎以添加新的运算符和类型