Currently, I am facing a challenge in creating a JSON object from an HTML table using JavaScript. While I can successfully retrieve the values of each cell in JavaScript, the issue lies in organizing and retrieving them as per my requirements. Here is the code snippet that illustrates how I am currently fetching the values:
var x = document.getElementById("tableId");
for (var i = 0; i < x.rows.length; i++) {
for (var j = 0; j < x.rows[i].cells.length; j++){
tableJsonDTO[name] = x.rows[i].cells[j].innerHTML;
}
}
The table structure is as follows:
header: company_1 company_2 company_3
Question1 answer_1 answer_1 answer_1
Question2 answer_2 answer_2 answer_2
I aim to extract the values to achieve an object structure like this:
var obj = [{spname:"company_1",qalist:[{question:"",answer:""},{question:"",answer:""}]},
{spname:"company_2",qalist:[{question:"",answer:""},{question:"",answer:""}]},
{spname:"company_3",qalist:[{question:"",answer:""},{question:"",answer:""}]}]
If you have any suggestions or solutions on how to approach this problem, please share them. Your input would be greatly appreciated.