Create an array of objects by iterating through table rows

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

    Try this code

    var trArray = [];
    $('#tbPermission tr').each(function () {
        var tr =$(this).text();  //get current tr's text
        var tdArray = [];
        $(this).find('td').each(function () {
            var td = $(this).text();  //get current td's text
            var items = {}; //create an empty object
            items[tr] = td; // add elements to object 
            tdArray.push(items); //push the object to array
        });    
    });
    

    Here, I just created an empty object, filled object with references of tr and td, the added that object to the final array.

    adding a working jsfiddle

提交回复
热议问题