Group Table Rows into an array
I need to group a variable amount of table rows into an array. Each group has 5 cells as shown in the HTML:
<tr>
<td width='23%' class='table-item-1'><strong>EVP</strong></td>
<td width='15%' class='table-item-1'>Diane</td>
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td>
<td width='17%' class='table-item-1'>xxx-xxx-6xxx</td>
<td width='25%' class='table-item-1'> <a href='mailto:'></a></td>
</tr>
<tr>
<td width='23%' class='table-item-1'><strong>EVP</strong></td>
<td width='15%' class='table-item-1'>Leslee</td>
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td>
<td width='17%' class='table-item-1'>xxx-xxx-6xxx</td>
<td width='25%' class='table-item-1'> <a href='mailto:'></a></td>
</tr>
<tr>
<td width='23%' class='table-item-1'><strong>Production Mgr</strong></td>
<td width='15%' class='table-item-1'><s>Lenny </s> tbh</td>
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td>
<td width='17%' class='table-item-1'>xxx-xxx-6xxx</td>
<td width='25%' class='table-item-1'> <a href='mailto:'></a></td>
</tr>
<tr>
<td width='23%' class='table-item-1'><strong>Production</strong></td>
<td width='15%' class='table-item-1'>Jessica</td>
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td>
<td width='17%' class='table-item-1'>xxx-xxx-2xxx</td>
<td width='25%' class='table-item-1'> <a href='mailto:'></a></td>
</tr>
Here is what I have so far:
var items_size = $('[width|="23%"]').size(); //gives me the amount of groups
var items1 = {
'name': $('.table-item-1 ~ td').html(),
'email': $('.table-item-1 ~ td > a').html(),
'phone': $('.table-item-1 ~ td').next().html(),
'fax': $('.table-item-1 ~ td').next().next().html(),
'title': $('.table-item-1 ~ td').prev().children().html()
}; // creates an array co开发者_如何学编程ntaining the items from the first group.
I need to create the same array for each group in the HTML.
var items = [];
$('table tr').each(function () {
// access each tr's tds from here using $(this)
var item = {
'name' : $(this).find('td:first').text(),
...
}
items.push(item);
});
精彩评论