Convert html table to array in javascript

后端 未结 3 769
借酒劲吻你
借酒劲吻你 2020-11-28 09:22

How can an HTML table be converted into a JavaScript array?

Item Descript
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 10:09

    This a function coverts an Html Table (just give the id) and it returns an array of the table :

                function table_to_array(table_id) {
                        myData = document.getElementById(table_id).rows
                        //console.log(myData)
                        my_liste = []
                        for (var i = 0; i < myData.length; i++) {
                                el = myData[i].children
                                my_el = []
                                for (var j = 0; j < el.length; j++) {
                                        my_el.push(el[j].innerText);
                                }
                                my_liste.push(my_el)
    
                        }
                        return my_liste
                }
    

    I hope it helps you !

提交回复
热议问题