什么函数在jQuery中充当.SelectMany()? [英] What function acts as .SelectMany() in jQuery?

查看:105
本文介绍了什么函数在jQuery中充当.SelectMany()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我解释一下:

我们知道jQuery中的map函数充当.Select()(如在LINQ中)。

we know that map function in jQuery acts as .Select() (as in LINQ).

$("tr").map(function() { return $(this).children().first(); }); // returns 20 tds

现在问题是我们如何在jQuery中使用.SelectMany()?

now the question is how can we have .SelectMany() in jQuery?

$("tr").map(function() { return $(this).children(); }); // returns 10 arrays not 20 tds!

这是我的实例: http://jsfiddle.net/8aLFQ/4/
如果我们有selectMany,
l2应为8。

here is my example in action: http://jsfiddle.net/8aLFQ/4/
"l2" should be 8 if we have selectMany.

[注意]请不要坚持这个例子,上面的代码只是显示我的意思是SelectMany()否则很容易说$(tr)。children() ;

[NOTE] please don't stick to this example, above code is to just show what I mean by SelectMany() otherwise it's very easy to say $("tr").children();

希望它足够清楚。

推荐答案

map 将展平原生数组。因此,您可以写:

map will flatten native arrays. Therefore, you can write:

$("tr").map(function() { return $(this).children().get(); })

你需要调用 .get ()返回本机数组而不是jQuery对象。

You need to call .get() to return a native array rather than a jQuery object.

这也适用于常规对象。

var nested = [ [1], [2], [3] ];
var flattened = $(nested).map(function() { return this; });

flattened 将等于 [1,2,3]

这篇关于什么函数在jQuery中充当.SelectMany()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆