每次添加时如何用对象数组填充html表

时间:2020-07-17 20:34:40

标签: javascript html loops for-loop html-table

我有这个对象数组 enter image description here

我想在添加Html表时随时添加此数据。 enter image description here

看起来像这样 enter image description here

我尝试使用此功能

function addDataToTbody(table, data) { // table -> NodeList, data -> array with objects
    data.forEach((d, i) => {
        var tr = table.insertRow(i);

        Object.keys(d).forEach((k, j) => { // Keys from object represent th.innerHTML
            var cell = tr.insertCell(j);
            cell.innerHTML = d[k]; // Assign object values to cells   
        });

        table.appendChild(tr);
    })
}

但是每次单击添加按钮时,它都会继续返回数组中的所有对象 enter image description here

请对此提供帮助。

0 个答案:

没有答案