How can I create an array of objects from an HTML table body? This is how my HTML looks like
<tbody id="metadata">
<tr>
<td><input type="numeric" /></td>
<td><input type="numeric" /></td>
</tr>
<tr>
<td><input type="numeric" /></td>
<td><input type="numeric" /></td>
</tr>
<tr>
<td><input type="numeric" /></td>
<td><input type="numeric" /></td>
</tr>
</tbody>
here is what I tried this javascript to get all the values from the table cell in an array of objects. I don't understand why my cMetadata always return a blank array
let cMetadata;
if (document.querySelectorAll("metadata")) {
let tableData = document.querySelectorAll("metadata");
cMetadata = [...tableData].map((row) => {
return {
altions: row.cells[0].textContent,
lexity: row.cells[1].textContent,
};
});
}
final array of object should look like
cMetadata = [{altions: 1 , lexity: 2},{altions: 2 , lexity: 2},{altions: 3 , lexity: 2}]
.querySelectorAll()for such trivial queries only hurts the performance of your app,document.getElementById()ordocument.getElementsByTagName()would work faster.tableDatavariable has to store<tbody>-node, you got missing '#' and are supposed to use.querySelector()(not 'all'). I.e..document.querySelector('#metadata')<tbody>you need to do further nested queries to capture<tr>,<td>nodes (to drill down to the values of nested<input>-nodes).document.querySelector('#metadata')but its not working