【问题标题】:How to insert values of objects in array into html table with javascript如何使用javascript将数组中对象的值插入到html表中
【发布时间】:2021-03-25 03:05:16
【问题描述】:

有人可以帮我创建用于循环遍历对象数组和遍历对象值的函数,但仅限于我需要的特定值(id、名字、电子邮件、角色、创建时间、更新时间),而不是全部。并将它们插入到 html 表中

数组:

(2) [{…}, {…}]
0: {_id: "5fd5efc8dfb1d434b84ea1c6", firstName: "ivan", lastName: "ivic", email: "ivan@gmail.com", role: "user", repeatPassword: "password" …}
1: {_id: "5fd74b0562af8926f44b1cfd", firstName: "pero", lastName: "peric", email: "pero@gmail.com", role: "user", repeatPassword: "password" …}

length: 2

表:

<table class="table my-0">
                    <thead>
                      <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Role</th>
                        <th>Created At</th>
                        <th>Updated At</th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                      </tr>
                    </tbody>
</table>

【问题讨论】:

  • 提示:DOM 操作
  • cratedAt 和 updated 不在您的数据中
  • 如果您认为其中一个答案有帮助,请将其标记为已接受。

标签: javascript html arrays object foreach


【解决方案1】:

由于某些列不在您的数据中,因此我将您的表格限制为数据中可见的字段。此外,您不需要初始 HTML 表中的空行。

使用insertRowinsertCell 方法:

let data = [
  { _id: "5fd5efc8dfb1d434b84ea1c6", firstName: "ivan", lastName: "ivic", email: "ivan@gmail.com", role: "user" },
  { _id: "5fd74b0562af8926f44b1cfd", firstName: "pero", lastName: "peric", email: "pero@gmail.com", role: "user" }
];

let table = document.querySelector(".my-0");
for (let obj of data) {
  let tr = table.insertRow();
  tr.insertCell().textContent = obj._id;
  tr.insertCell().textContent = obj.firstName + " " + obj.lastName;
  tr.insertCell().textContent = obj.email;
  tr.insertCell().textContent = obj.role;
}
table, td, th { border: 1px solid }
table { border-collapse: collapse }
<table class="table my-0">
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Email</th>
      <th>Role</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

【讨论】:

    【解决方案2】:

    innerHTML 解决方案

     const ar = [{ _id: "5fd5efc8dfb1d434b84ea1c6", firstName: "ivan", lastName: "ivic", email: "ivan@gmail.com", role: "user", repeatPassword: "password" },
         { _id: "5fd74b0562af8926f44b1cfd", firstName: "pero", lastName: "peric", email: "pero@gmail.com", role: "user", repeatPassword: "password" }
     ]
     let result = `<table><thead>
                   <tr><th>ID</th><th>Firstname</th><th>E-Mail</th><th>Role</th></tr></thead><tbody>`;
     ar.forEach((elem) => {
         result += `<tr><td>${elem._id}</td><td>${elem.firstName}</td><td>${elem.email}</td><td>${elem.role}</td></td></tr>`
    
     });
     result += `</tbody></table>`;
     document.getElementById("toIns").innerHTML = result;
     &lt;div id="toIns"&gt; &lt;/div&gt;

    【讨论】:

    • 看看输出...不对。
    • 我已经更新了,必须连接它。 Ty 的建议
    • 这是真的 ^^ :D
    【解决方案3】:

    您的解决方案是一个简单的循环:

    // Data mock
    const users = [
      {_id: "5fd5efc8dfb1d434b84ea1c6", firstName: "ivan", lastName: "ivic", email: "ivan@gmail.com", role: "user", repeatPassword: "password", created_at: "date", updated_at: "date"},
      {_id: "5fd74b0562af8926f44b1cfd", firstName: "pero", lastName: "peric", email: "pero@gmail.com", role: "user", repeatPassword: "password", created_at: "date", updated_at: "date"}
    ];
    // End data mock
    
    
    const tableBody = document.getElementById('table-body');
    
    for (const user of users) {
      const tr = document.createElement('tr');
      const content = `<td>${user._id}</td>
      <td>${user.firstName}</td>
      <td>${user.email}</td>
      <td>${user.role}</td>
      <td>${user.created_at}</td>
      <td>${user.updated_at}</td>`;
      
      tr.innerHTML = content;
      tableBody.appendChild(tr)
     }
    <table class="table my-0">
      <thead>
        <tr>
          <th>ID</th>
          <th>Name</th>
          <th>Email</th>
          <th>Role</th>
          <th>Created At</th>
          <th>Updated At</th>
        </tr>
      </thead>
      <tbody id="table-body">
      </tbody>
    </table>

    【讨论】:

    • 我试过{ _id: "12&lt;abc", ... }...但结果不正确。
    • 您可以用 innerText 替换 innerHTML。我猜第一个带有一些逃逸的字符。在这里,需要的是没有特殊字符的 ID,所以我推荐使用 innerHTML。为什么 ID 应该有特殊字符?
    【解决方案4】:

    let arr = [{_id: "5fd5efc8dfb1d434b84ea1c6", firstName: "ivan", lastName: "ivic", email: "ivan@gmail.com", role: "user", repeatPassword: "password"},
    {_id: "5fd74b0562af8926f44b1cfd", firstName: "pero", lastName: "peric", email: "pero@gmail.com", role: "Admin", repeatPassword: "password"}]
    
    function addItems(){
       let tbody = document.querySelector('tbody');
       let rows = arr.map(e => {
           let td = document.createElement('tr');
           let id = `<td>${e._id}</td>`
           let first = `<td>${e.firstName}</td>`
           let last = `<td>${e.lastName}</td>`
           let email = `<td>${e.email}</td>`
           let role = `<td>${e.role}</td>`
           //let createdAt = `<td>${e.createdAt}</td>` you still have to add created at field
           td.innerHTML = id+first+last+email+role
           return td
       })
      rows.forEach(n => tbody.append(n))
    }
    
    addItems()
    <table class="table my-0">
                        <thead>
                          <tr>
                            <th>ID</th>
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th>Email</th>
                            <th>Role</th>
                          </tr>
                        </thead>
                        <tbody>
                          
                        </tbody>
    </table>

    【讨论】:

    • 我试过{ _id: "12&lt;abc", ... }...但结果不正确。
    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 2019-01-16
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多