Using .map()
with .get()
is one way to go:
var arr = $(value).map(function() {
var $th = $(this);
if($th.is("TD")) return $th.html();
}).get();
alert(arr);
I'm not sure what value
represents, but if you change the selector to match only td
elements, you could simplify the return statement with return $(this).html();
.
.map()
iterates over the elements, and adds the return value to the jQuery object. .get()
retrieves just the array out of the jQuery object.
Sounds like value
is a tr
. Then you could do this:
var arr = $(value).children('td').map(function() {
return $(this).html();
}).get();
alert(arr);
To create an array with each item containing an array of that row's td
element's html, you could do this:
var arr = [];
$('tr').each(function() {
arr.push($(this).children('td').map(function() {
return $(this).html();
}));
}).get();
console.log(arr);
This uses the standard .push()
since I don't think that using .map()
inside .map()
would work. I think when you pass the inner array into the jQuery object, it just adds it to the main array (or something).