for循环中不存在变量

该方案增加了表行for each客户端和内部的table row,一个td客户端的每一个值(如姓名,IP,值等)。这些值和客户端设置在一个key/value object或dictionary称为clientel.


我已将程序设置如下:


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)

  }

};

我有按 em' 时执行功能的按钮,但出于某种原因,它说clientel未定义?


人到中年有点甜
浏览 312回答 1
1回答

慕斯王

你不能在循环中分配i和。您需要使用来获取属性值。clientfor-inclientel[i]let clientel = {&nbsp; socket101: ['Rick', '192.590.49.1', 'Win10', 'Norway', '15:49.00'],&nbsp; socket102: ['Anthony', '192.90.897.0', 'Win7', 'Negritine', '19:19:38']};function man_table() {&nbsp; const table = document.getElementById('table-body');&nbsp; for (let i in clientel) {&nbsp; &nbsp; let row = table.insertRow(i);&nbsp; &nbsp; let index = row.insertCell(0);&nbsp; &nbsp; let name = row.insertCell(1);&nbsp; &nbsp; let ip = row.insertCell(2);&nbsp; &nbsp; let os = row.insertCell(3);&nbsp; &nbsp; let country = row.insertCell(4);&nbsp; &nbsp; let timee = row.insertCell(5);&nbsp; &nbsp; let client = clientel[i];&nbsp; &nbsp; index.innerHTML = i;&nbsp; &nbsp; name.innerHTML = client[0];&nbsp; &nbsp; ip.innerHTML = client[1];&nbsp; &nbsp; os.innerHTML = client[2];&nbsp; &nbsp; country.innerHTML = client[3];&nbsp; &nbsp; timee.innerHTML = client[4];&nbsp; }};function roomba() {&nbsp; for (let client in clientel) {&nbsp; &nbsp; console.log(client)&nbsp; }};man_table();<table id="table-body"></table>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript