Create an array of objects by iterating through table rows

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

    I would suggest changing your html slightly.

    User ID User Name
    1 Test1

    Then in your javascript when you want to get all the elements as an array you could do.

    var userIdArray = $('#tbPermission .userid').map(function(userid){ return $(userid).html(); }).toArray();
    

    This will find all elements with a class userid on the table, collect just the values, and .toArray() that result to get a basic javascript array. You can then take that and manipulate it into whatever json structure you want, or you could possibly create your json object inside that map function.

提交回复
热议问题