Create an array of objects by iterating through table rows

后端 未结 5 2025
鱼传尺愫
鱼传尺愫 2021-01-27 04:46

I have an HTML table and I want to iterate through its rows and create a collection or lets say an \"array of objects\".

For example:

5条回答
  •  粉色の甜心
    2021-01-27 04:58

    It's a bit tricky based on the given structure. You may have to modify the HTML a bit to map cells to headers, like below.

    var myArray = [];
      
    $("#tbPermission").find("td").each(function() {
        var $this = $(this), obj = {};
        obj[$this.data("column")] = $this.text();
        myArray.push(obj);
    });
    
    alert ( JSON.stringify(myArray) );
    
    
    User ID User Name
    1 Test1

    Please give in some time to learn about Array.push() and Objects in Javascript. Hope that helps.

提交回复
热议问题