Goal: get all table row (cell) data and add to an array
Problem: if there are more than 1 row, it over-writes previously added array entries in stead of adding it to the end of the array. This causes the array to have the last table row dataset duplicated/triplicated/etc (as many rows as there exist in table)
Setup: I have a dynamic html table where a user can add rows by entering a user name and age and click the Add button. The Load button must get the table body elements and populate an object, where after a for loop must get the innerText, update the object and push() the object to the array. But this is the result: Screenshot of console.log of array
let roleArr = [];
loadBtn.addEventListener("click", () => {
roleTableData();
});
function roleTableData() {
let test = tblRows.children;
const collectRoles = {
uName: "",
uAge: "",
};
for (let i = 0; i < test.length; i++) {
collectRoles.uName= test[i].children[0].innerText;
collectRoles.uAge= test[i].children[1].innerText;
roleArr.push(collectRoles);
}
}