Given the html
<div class="recipeList">
<div class="recipe" data-id="abc"></div>
<div class="recipe" data-id="def"></div>
<div class="recipe" data-id="ghi"></div>
<div class="recipe" data-id="jkl"></div>
</div>
and the jQuery
$('.recipeList').find('.recipe')
.map(function(index, element) {
return [
$(element).data('id'),
{ sequence: index }
]
}).get()
I would expect to get
[
['abc',{sequence:0}],
['def',{sequence:1}],
['ghi',{sequence:2}],
['jkl',{sequence:3}]
]
but instead I get
[
'abc',
{sequence:0},
'def',
{sequence:1},
'ghi',
{sequence:2},
'jkl',
{sequence:3}
]
Is there a way to map a jQuery's selected elements to a 2d array, i.e., how can I get the expected output?
(Obviously I can manipulate the 1d array returned by .map()
, to generate my expected output, but I'd rather not, and I'm also curious about this behavior of .map()
: why is it producing this result, and is there a way to get it to return a 2d array instead?)