I'm need to extract data from a html table to create a JSON array.
Here is the structure of HTML table,
<table class="tableClass">
<thead class="tableHeaderClass" >
<tr>
<th>header_column1</th>
<th>header_column2</th>
<th>header_column3</th>
</tr>
</thead>
<tbody class="tableBodyClass">
<tr>
<td>row1_column1</td>
<td>row1_column2</td>
<td>row1_column3</td>
</tr>
<tr>
<td>row2_column1</td>
<td>row2_column2</td>
<td>row2_column3</td>
</tr>
</tbody>
</table>
In my JavaScript function, I'm doing this
function() {
var json = {
header_column1 : '',
header_column2 : '',
header_column3 : ''
};
var data = [];
$('tableClass').find('tbody').children('tr').each(function() {
var $tds = $(this).find('td');
json.header_column1 = $tds.eq(0).text();
json.header_column2 = $tds.eq(1).text();
json.header_column3 = $tds.eq(2).text();
data.push(json);
});
return data;
}
when I print my array, but I'm getting only 'row2_column1, row2_column2, row2_column3'.
Couldn't workout what I'm doing wrong/missing. Would be great if you could help me out with this.