简体   繁体   中英

Select all thead and tr has td except the last column

i make this code to get all the value of the table and to insert in array with index name.

codes work well, but i want to exclude the last column of the table.

                var array = [];
                var headers = [];
                $('#idTable th').each(function(index, item) 
                {
                    headers[index] = $(item).html();
                });

                $('#idTable tr').has('td').each(function() 
                {
                    var arrayItem = {};
                    $('td', $(this)).each(function(index, item) 
                    {
                        arrayItem[headers[index]] = $(item).html();
                    });
                    array.push(arrayItem);
                });

i already tried not(:last-child) but the last column still included.

check my jsfiddle. https://jsfiddle.net/del17/1yx3csrw/3/

This should do what you want:

 var array = []; var headers = []; $('#idTable th:not(:last-child)').each(function() { headers.push($(this).text()) }) $('#idTable tbody tr').each(function() { var arrayItem = {}; $(this).find('td:not(:last-child)').each(function(i) { arrayItem[headers[i]] = $(this).text(); }) array.push(arrayItem); }); console.log(array) console.log(headers)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="idTable"> <thead> <tr> <th>A</th> <th>B</th> <th>C</th> <th>D</th> </tr> </thead> <tbody> <tr> <td>a1</td> <td>b1</td> <td>c1</td> <td>d1</td> </tr> <tr> <td>a2</td> <td>b2</td> <td>c2</td> <td>d2</td> </tr> </tbody> </table>

I think for what you want to do it's better to use "text" instead of "html".

Also, don't use index too much if you know you iterate through the same order.

It's better to use "push" if you want to add elements to an array like you seem to do here.

here is fiddle: https://jsfiddle.net/p0qu5mc9/

var array = [];
                    var headers = [];
                    var columnlength = document.getElementById('idTable').rows[0].cells.length; 
                    $('#idTable th').each(function(index, item) 
                    {

                        if(columnlength-1 > index){
                        headers[index] = $(item).html();
                        }

                    });

                    $('#idTable tr').has('td').each(function() 
                    {
                        var arrayItem = {};
                        $('td', $(this)).each(function(index, item) 
                        {   
                            if(columnlength-1 > index){
                            arrayItem[headers[index]] = $(item).html();
                            }
                        });
                        array.push(arrayItem);
                    });

                    alert(JSON.stringify(array));

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