0

I want to convert the data of a html table into json set its key.

var tbl = $('#table-availed-prod tr').map(function () {
                    return $(this).find('td').map(function () {
                        return $(this).html();
                    }).get();
                }).get();

What I get is this

JSON :

IMAGE

OR

Array(){
0: "Sophos"
1: "Complementary"
2: "Codey Ropen"
}

What I need is something like this

JSON :

[{
Productname:"Sophos",
ProductTypename:"Software",
AssignedPerson:"Codey"
},
{
Productname:"Sophos",
ProductTypename:"Software",
AssignedPerson:"Codey"
},
{
Productname:"Sophos",
ProductTypename:"Software",
AssignedPerson:"Codey"
}]

My Table: Table Image

ropenrom24
  • 517
  • 8
  • 18

1 Answers1

3

Try this approach.

var arr1 = [];
var carr = ['product name', 'product type', 'assigned person'];
$("#table-availed-prod tr").map(function(i, tr){
 var arr = {};
 $(this).find('td').map(function(j, td){
       if(carr.indexOf(j) !== -1){
   arr[carr[j]] = $(this).text();
  }
 });
 arr1.push(arr);
});

console.log(arr1);
Prabu samvel
  • 1,213
  • 8
  • 19