繁体   English   中英

为什么我没有得到包含以下代码的表格?

[英]Why am I not getting a table with the following code?

let myLibrary = [
                  ["Title", "Author", "Pages", "Have you read the book"]
                  ["Harry Potter", "JK Rowling", "366", No],
                  ["My Test Book", "Myself", "50", Yes]
                ];

  function render(){
  let table = document.getElementById('table');
  for(let book in myLibrary){
   let row = table.insertRow();
    for(let bookInfo in book){
      let cell = row.insertCell()
      cell.innerHTML = bookInfo;
    }
  }
}

我期待我会得到一个包含以下代码的表格。 但我刚开始 0 作为输出。 任何人都可以找到解决方案吗?

您可以使用forEach来迭代数组:

function render() {
  let table = document.getElementById("table");
  myLibrary.forEach(function (r) {
    let row = table.insertRow();
    r.forEach(function (c) {
      let cell = row.insertCell();
      cell.innerHTML = c;
    });
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM