簡體   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