0

How To Extract The Data From The Html Table and store it in jquery Array?

2
  • here is an example: jsbin.com/oveva3/1465/edit Commented Aug 7, 2014 at 5:55
  • @user2451349 What happen OP you got the answer? Commented Aug 7, 2014 at 9:19

2 Answers 2

3

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);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.