<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 ?
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/
var array = new Array();
$("td .unit-item").each(function(){
array.push($(this).html());
});
You could use the .map function. I'm on a mobile device right now so can't test it, but something like the below should work:
var result = jQuery("table tr").map(function () {return jQuery(this).find("td").map(function () { return jQuery(this).text(); }) });
Posting a more general solution that will work for an arbitrary number of rows.
Working Demo
var bArray = [];
$('tr').has('td.unit-item').each(function() {
bArray.push($(this).find('td.unit-item').map(function() {
return $.trim(this.innerHTML);
}).get());
});
for(var i = 0; i < bArray.length; i++) {
console.log(bArray[i]);
}
精彩评论