Reputation: 51
How To Extract The Data From The Html Table and store it in jquery Array?
Upvotes: 0
Views: 180
Reputation: 2424
I thought of doing it using jquery map
CODE :
var varObject = $('td').map(function(){
return $(this).text();
}).get();
Now convert it into real array by using jquery makeArray and if you want to check it is valid array use isArray
var array = $.makeArray(varObject);
if(jQuery.isArray( array ) === true){
console.log(' valid array ');
}
Here is a SAMPLE DEMO.
Upvotes: 1
Reputation: 60448
If you want to convert a html table to a javascript array, you can use something like this.
Html
<table id="myTableId">
<tr>
<td>Row 1 - Column 1</td>
<td>Row 1 - Column 2</td>
</tr>
<tr>
<td>Row 2 - Column 1</td>
<td>Row 2 - Column 2</td>
</tr>
</table>
jQuery
var tableArray = [];
$("table#myTableId tr").each(function() {
var rowData = [];
var tableData = $(this).find('td');
if (tableData.length > 0) {
tableData.each(function() { rowData.push($(this).text()); });
tableArray.push(rowData);
}
});
Upvotes: 3