Create an array of objects by iterating through table rows

后端 未结 5 1919
鱼传尺愫
鱼传尺愫 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:46

    Check the console, you will get an array with the desired objects

    var arr = [];
    $('#tbPermission tr:not(.header)').each(function() {
      var that = $(this);
      var id = that.find('td').eq(0).text();
      var name = that.find('td').eq(1).text();
      var obj = { 'userId': id , 'userName': name  };
      arr.push(obj);
    });
    console.log(arr);
    
    
    User ID User Name
    1 Test1
    2 Test2

提交回复
热议问题