Convert Table to an Array

后端 未结 5 1341
挽巷
挽巷 2020-12-14 04:43

I have seen many posts about how to turn an array into a table, but not nearly as many going the other way. I\'m looking to take a table like this:




        
5条回答
  •  一向
    一向 (楼主)
    2020-12-14 05:32

    There's a good tool: $(table).map, but saldly, it always return flatten array, no matter how many nested .map() is inside (you need two: one for rows and one for cells).

    So let's use jQuery.map for tables and tr's and object.map() for cells, having 2nd nesting level return an array

    function t2a (tabble){
    return $.map($(tabble),function(el,i) {
        return $.map($(el).find('tr'),function(el,i) {
            return [
                $(el).find('td').map(function() {
                    return $(this).text();
                }).get()
            ];
        });
    });
    }
    

提交回复
热议问题