简体   繁体   中英

How to get all value from table row and column to array in jquery

How to get all value from table row and column to array

field1         field2           field3
  1             aaa              aaa
  2             bbb              bbb
  3             ccc              ccc
  4             ddd              ddd

i want to get value from table row to array like below:

array1=[["1","aaa","aaa"],["2","bbb","bbb"],["3","ccc","ccc"],["4","ddd","ddd"]];

My html

<table>
    <thead>
        <tr>
            <th>field1</th>
            <th>field2</th>
            <th>field3</th>
        </tr>
    </thead>
        <tr>
            <td>1</td>
            <td>aaa</td>
            <td>aaa</td>
        </tr>
        <tr>
            <td>2</td>
            <td>bbb</td>
            <td>bbb</td>
        </tr>
        <tr>
            <td>3</td>
            <td>ccc</td>
            <td>ccc</td>
        </tr>
        <tr>
            <td>4</td>
            <td>ddd</td>
            <td>ddd</td>
        </tr>
</table>

Any solution for this? Thank you

Your can try:

 function get(){ var table = $('table'); var data = []; table.find('tr').each(function (i, el) { // no thead if( i != 0){ var $tds = $(this).find('td'); var row = []; $tds.each(function (i, el){ row.push($(this).text()); }); data.push(row); } }); return data; } console.log(get()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <thead> <tr> <th>field1</th> <th>field2</th> <th>field3</th> <th>field4</th> </tr> </thead> <tr> <td>1</td> <td>aaa</td> <td>aaa</td> </tr> <tr> <td>2</td> <td>bbb</td> <td>bbb</td> </tr> <tr> <td>3</td> <td>ccc</td> <td>ccc</td> </tr> <tr> <td>4</td> <td>ddd</td> <td>ddd</td> </tr> </table> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM