变量在for循环中不存在

时间:2019-06-03 22:14:00

标签: javascript html

该程序将表行for each客户端添加到该行table row内,为客户端的每个值(例如名称,ip,值等)添加一个td。 这些值和客户端在称为key/value object的{​​{1}}或dictionary中设置。

我将程序设置如下:

clientel

当我按em'时,我有一些按钮可以执行功能,但是由于某种原因,它说let clientel = { socket101: ['Rick', '192.590.49.1', 'Win10', 'Norway', '15:49.00'], socket102: ['Anthony', '192.90.897.0', 'Win7', 'Negritine', '19:19:38'] }; function man_table() { const table = document.getElementById('table-body'); for (let i, client in clientel) { let row = table.insertRow(i); let index = row.insertCell(0); let name = row.insertCell(1); let ip = row.insertCell(2); let os = row.insertCell(3); let country = row.insertCell(4); let timee = row.insertCell(5); index.innerHTML = i; name.innerHTML = client[0]; ip.innerHTML = client[1]; os.innerHTML = client[2]; country.innerHTML = client[3]; timee.innerHTML = client[4]; } }; function roomba() { for (let client in clientel) { console.log(client) } };没有定义?

1 个答案:

答案 0 :(得分:1)

您无法在i循环中分配clientfor-in。您需要使用clientel[i]来获取属性值。

let clientel = {
  socket101: ['Rick', '192.590.49.1', 'Win10', 'Norway', '15:49.00'],
  socket102: ['Anthony', '192.90.897.0', 'Win7', 'Negritine', '19:19:38']
};

function man_table() {
  const table = document.getElementById('table-body');
  for (let i in clientel) {
    let row = table.insertRow(i);
    let index = row.insertCell(0);
    let name = row.insertCell(1);
    let ip = row.insertCell(2);
    let os = row.insertCell(3);
    let country = row.insertCell(4);
    let timee = row.insertCell(5);

    let client = clientel[i];
    index.innerHTML = i;
    name.innerHTML = client[0];
    ip.innerHTML = client[1];
    os.innerHTML = client[2];
    country.innerHTML = client[3];
    timee.innerHTML = client[4];
  }
};

function roomba() {
  for (let client in clientel) {
    console.log(client)
  }
};

man_table();
<table id="table-body">
</table>