Eduard Eduard - 1 month ago 7
jQuery Question

Storing the data from two rows in array jquery

<table>
<tbody>
<tr class="center">
<td></td>
<td width="35"></td>
<td width="35"></td>
<td width="35"></td>
<td width="35"></td>
<td width="35"></td>
<td width="35"></td>
<td width="35"></td>
<td width="35"></td>
<td width="35"></td>
<td width="35"></td>
<td width="35"></td>
<td width="35"></td>
</tr>
<tr>
<td width="20%">
Numar:
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
</tr>
<tr>
<td align="left" width="20%">
Pierderi:
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
<td style="text-align:center" class="unit-item hidden">
0
</td>
</tr>
</tbody>
</table>


I have this table, that I cannot modify, how can I store the numbers from those two rows into two arrays with jQuery ?

Answer

This will only work if you want the numbers from the last two rows in a three row table -

var arrone = [];
var arrtwo = [];

$("table tr").each(function(i) {
    $(this).find('td.unit-item').each(function() {
        i == 1 ? arrone.push($.trim($(this).text())) : arrtwo.push($.trim($(this).text()));
    })
})

console.log(arrone.join(','));
console.log(arrtwo.join(','));

Working demo - http://jsfiddle.net/ipr101/qRSDM/

Here's a slightly terser example using map -

var arrone = $("table tr:nth-child(2)").find('td.unit-item').map(function() {
    return $.trim($(this).text())
});

var arrtwo = $("table tr:nth-child(3)").find('td.unit-item').map(function() {
    return $.trim($(this).text())
});

console.log(arrone.get().join(','));
console.log(arrtwo.get().join(','));

Working demo - http://jsfiddle.net/ipr101/XjXaj/1/