What function acts as .SelectMany() in jQuery? What function acts as .SelectMany() in jQuery? jquery jquery

What function acts as .SelectMany() in jQuery?


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

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

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

This will work on regular objects as well.

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

flattened will equal [1, 2, 3].


You want this:

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

Live demo: http://jsfiddle.net/8aLFQ/12/


You're going to kick yourself:

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

It's the simple things in life you treasure. -- Fred Kwan

EDIT:Wow, that will teach me to not to test answers thoroughly.

The manual says that map flattens arrays, so I assumed that it would flatten an array-like object. Nope, you have to explicit convert it, like so:

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

Things should be as simple as possible, but no simpler. -- Albert Einstein