如何在JS表中选择元素?

C0mpl3x

1,我试图通过JS创建一个表格,但我遇到了一个小问题,我试图代替?,ID,所以它为我填写了ID,我只是写了id,但是我没有不知道该如何提及或接受它。

2.Ive尝试将其放在“”和“”中。

   var Applications =

[
    {cluster: 'WV', ID: 'UID8', Name: ' (BV)'},
    {cluster: 'WV', ID: 'UDWD', Name: 'AWS'},
    {cluster: 'WV', ID: 'UAD5', Name: 'AMS'},
    {cluster: 'WV', ID: 'U548', Name: 'COine'},
    {cluster: 'WV', ID: 'UADW', Name: 'GAine'},
    {cluster: 'WV', ID: 'UADA', Name: 'EP'},
    {cluster: 'WV', ID: 'UAHD', Name: 'ES'},
    {cluster: 'WV', ID: 'UHH5', Name: 'GTR'},
    {cluster: 'WV', ID: 'UHHA', Name: 'SPA '},
];
var txt= "";
Applications.forEach(table);
function table(item, index, arrays) {

    txt =  txt + "<tr><td>?????</td></tr>";

}
document.getElementById("tbl").innerHTML = txt;

科比

table()调用forEach(),您会Applications在forEach的每次迭代中遍历内部的每个对象您可以使用参数在函数内部访问对象item由于id是一个嵌套值,因此您可以像这样访问它item.ID

var Applications = [
    {cluster: 'WV', ID: 'UID8', Name: ' (BV)'},
    {cluster: 'WV', ID: 'UDWD', Name: 'AWS'},
    {cluster: 'WV', ID: 'UAD5', Name: 'AMS'},
    {cluster: 'WV', ID: 'U548', Name: 'COine'},
    {cluster: 'WV', ID: 'UADW', Name: 'GAine'},
    {cluster: 'WV', ID: 'UADA', Name: 'EP'},
    {cluster: 'WV', ID: 'UAHD', Name: 'ES'},
    {cluster: 'WV', ID: 'UHH5', Name: 'GTR'},
    {cluster: 'WV', ID: 'UHHA', Name: 'SPA '},
];

var txt = "";

Applications.forEach(table);

function table(item, index, arrays) {

  txt = txt + "<tr><td>" + item.ID + "</td></tr>";

}

document.getElementById("tbl").innerHTML = txt;
<table id="tbl"></table>

您可以使用reduce以下代码简化代码

var Applications = [
    {cluster: 'WV', ID: 'UID8', Name: ' (BV)'},
    {cluster: 'WV', ID: 'UDWD', Name: 'AWS'},
    {cluster: 'WV', ID: 'UAD5', Name: 'AMS'},
    {cluster: 'WV', ID: 'U548', Name: 'COine'},
    {cluster: 'WV', ID: 'UADW', Name: 'GAine'},
    {cluster: 'WV', ID: 'UADA', Name: 'EP'},
    {cluster: 'WV', ID: 'UAHD', Name: 'ES'},
    {cluster: 'WV', ID: 'UHH5', Name: 'GTR'},
    {cluster: 'WV', ID: 'UHHA', Name: 'SPA '},
];

var txt = Applications.reduce((acc, item) => acc + "<tr><td>" + item.ID + "</td></tr>", '');

document.getElementById("tbl").innerHTML = txt;
<table id="tbl"></table>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章