I'm having a hard time understanding the behaviour of jQuery's .toArray()
in this scenario.
I have 2 <table>
, one for "Participants" and one for "Winners".
I want to extract some data from these tables using jQuery.
I use 2 .map()
functions to transform the contents of the table into arrays/objects, have a look at the code:
HTML:
<h2>Participants</h2>
<table border="1">
<tbody>
<tr>
<td>01</td>
<td>Andrew</td>
</tr>
<tr>
<td>02</td>
<td>Julian</td>
</tr>
<tr>
<td>03</td>
<td>Matt</td>
</tr>
<tr>
<td>04</td>
<td>Sarah</td>
</tr>
</tbody>
</table>
<h2>Winners</h2>
<table border="1">
<tbody>
<tr>
<td>01</td>
<td>Andrew</td>
</tr>
<tr>
<td>04</td>
<td>Sarah</td>
</tr>
</tbody>
</table>
JavaScript:
const result = $('table')
.map((i, table) => {
return $(table)
.find('tbody tr')
.map((j, row) => {
const $row = $(row);
return {
number: $row.find('td:nth-child(1)').text(),
name: $row.find('td:nth-child(2)').text(),
};
})
.toArray(); // [1]
})
.toArray();
console.log(result);
[1] Without the .toArray()
I get an array with 2 jQuery objects, one for each table. (Great!)
But when I add .toArray()
all of a sudden it returns a single array with all the objects inside, why?
Where's the separation the was there before?!
Expected output:
[
[
{number: "01", name: "Andrew"},
{number: "02", name: "Julian"},
{number: "03", name: "Matt"},
{number: "04", name: "Sarah"}
],
[
{number: "01", name: "Andrew"},
{number: "04", name: "Sarah"}
]
]
Actual output:
[
{number: "01", name: "Andrew"},
{number: "02", name: "Julian"},
{number: "03", name: "Matt"},
{number: "04", name: "Sarah"},
{number: "01", name: "Andrew"},
{number: "04", name: "Sarah"}
]
I made a jsfiddle so you can see for yourself: https://jsfiddle.net/93cLyq6h/22/