Home > database >  How to map specific HTML table columns and get its data into an array (JS)?
How to map specific HTML table columns and get its data into an array (JS)?

Time:06-16

I'm trying to get the table data from some columns and I've tried a couple of variations I could think of, but none worked:

function updateAllTasks() {
var table = document.getElementById("dtable");

var [, ...tr] = table.querySelectorAll("tr");
var tableData = [...tr].map(r => {
var td = r.querySelectorAll("td");
    console.log([...td].map(c => c[0].innerHTML, c[1].innerHTML, c[8].querySelectorAll('input[name="rowcheckbox"]')[0].value)); //This is the one giving me error ReferenceError: c is not defined
})
console.log(JSON.stringify(tableData));
}

Thanks for your help!

CodePudding user response:

As mentioned by Andy, following data structures logic. The best approach for this case will be to call the data inside of an Array, and after that, build the table with the data.

This will provide more flexibility the moment you process the information.

Reference

  • Related