How to convert HTML table to Javascript Object with jQuery

后端 未结 6 1054
夕颜
夕颜 2020-12-02 17:49

I am trying to convert this HTML table:

\"enter

Code:

6条回答
  •  -上瘾入骨i
    2020-12-02 18:01

    See updated fiddle. The additional array map is unnecessary because you are looking for a literal object for your JSON at this point.

    var data = $('table#students tbody tr').map(function(index) {
        var cols = $(this).find('td');
        return {
            id: index + 1,
            name: cols[0].innerHTML,            // use innerHTML
            age: (cols[1].innerHTML + '') * 1,  // parse int
            grade: (cols[2].innerHTML + '') * 1 // parse int
        };
    }).get();
    

提交回复
热议问题