【问题标题】:List of objects to HTML unordered list对象列表到 HTML 无序列表
【发布时间】:2022-01-14 03:26:30
【问题描述】:

任务的目标是从下面给出的对象列表中创建一个无序的 HTML 列表。

到目前为止我的设置:

const users = [
  { name: "Helene", age: 54, email: "helene@helene.com", },
  { name: "Janet", age: 24, email: "janet@janet.com", },
  { name: "Michel", age: 25, email: "michel@michel.com",}
];

const div = document.querySelector('.app');
let usersName = [];
let usersEmails = [];
let usersAge = [];

for (let i = 0; i < users.length; i++) {
  usersEmails.push(users[i].email);
  usersName.push(users[i].name);
  usersAge.push(users[i].age);;
}
for (let i = 0; i < users.length; i++) {
  var ul = document.createElement('ul');
  div.appendChild(ul);

  for (let i = 0; i < 3; i++) {
    var li = document.createElement('li');
    li.innerHTML = [usersName[i], usersAge[i], usersEmails[i]];
    ul.appendChild(li);
  }
}
&lt;div class="app"&gt;&lt;/div&gt;

我遇到的问题是信息都堆叠在一个'li'元素中,我如何制作它以便每个对象都有自己的'li'?

预期输出:

  <ul>
    <li>Helene</li>
    <li>54</li>
    <li>helene@helene.com</li>
  </ul>
  <ul>
    <li>Janet</li>
    <li>24</li>
    <li>janet@janet.com</li>
  </ul>
  <ul>
    <li>Michel</li>
    <li>25</li>
    <li>michel@michel.com</li>
  </ul>

不胜感激。谢谢

【问题讨论】:

  • 移除外部 for 循环。您基本上是在创建 3 个列表,每个用户 1 个。
  • 请提供您预期输出的示例。目前我们只知道您想要什么。 :-)
  • 您想为每个用户创建一个列表,还是为所有用户创建一个列表?

标签: javascript html object dom html-lists


【解决方案1】:

如果目标是创建一个用户列表,其中每个用户只出现一次,并且所有项目都出现在同一行,则只需一个循环即可获取用户,并生成 li 项目。

const users = [{"name":"Helene","age":54,"email":"helene@helene.com"},{"name":"Janet","age":24,"email":"janet@janet.com"},{"name":"Michel","age":25,"email":"michel@michel.com"}];

const ul = document.querySelector('.app');

for (let i = 0; i < users.length; i++) {
  const user = users[i]; // user from list by index
  const li = document.createElement('li');
  li.innerHTML = `${user.name}, ${user.age}, ${user.emails}`; // create the string for the user
  ul.appendChild(li);
}
&lt;ul class="app"&gt;&lt;/ul&gt;

如果要创建用户属性的子列表,则需要在li 内创建另一个ul,使用for...in 迭代对象属性,然后为每个对象创建li 项属性:

const users = [{"name":"Helene","age":54,"email":"helene@helene.com"},{"name":"Janet","age":24,"email":"janet@janet.com"},{"name":"Michel","age":25,"email":"michel@michel.com"}];

const ul = document.querySelector('.app');

for (let i = 0; i < users.length; i++) {
  const user = users[i]; // user from list by index
  const li = document.createElement('li');
  const subUl = document.createElement('ul');
  ul.appendChild(li);
  li.appendChild(subUl);
  
  for(const key in user) {
    const val = user[key];
    const subLi = document.createElement('li');
    
    subLi.innerHTML = `${key} - ${val}`;
    subUl.appendChild(subLi);
  }
}
&lt;ul class="app"&gt;&lt;/ul&gt;

【讨论】:

  • 为什么要删除第一个将用户属性打包到单独数组中的循环?为什么你认为你的答案是 OP 真正想要的?
  • @Andreas 根据问题中的代码,您认为三个单独的数组有什么用处?
  • @Andy 我不是 OP。只有 OP 知道为什么使用该设置。它可能有一个目的。可能不会。在澄清之前,任何答案都只是猜测
  • @Andy 和 Andreas - 谢谢你们的 cmets。至于话题本身。许多用户从不知道他们真正需要什么开始,或者很难解释自己。过去,我曾与用户进行过长时间的讨论,但最终都没有结果。我通常会创建一个适合我的答案,并在问题发生变化或用户对我的答案做出反应时进行更改。
  • @OriDrori 我也是。这就是为什么我的回答和你的回答最初是一样的,然后因为你贯彻了你的想法而对你投了赞成票。
【解决方案2】:

如果您的问题是重复用户列表,并且您只想为所有用户提供一个列表,您可以轻松删除对用户的第二次迭代,如下所示:

const users = [{
  name: "Helene",
  age: 54,
  email: "helene@helene.com",
}, {
  name: "Janet",
  age: 24,
  email: "janet@janet.com",
}, {
  name: "Michel",
  age: 25,
  email: "michel@michel.com",
}];


const div = document.querySelector('.app');
let usersName = [];
let usersEmails = [];
let usersAge = [];

for (let i = 0; i < users.length; i++) {
  usersEmails.push(users[i].email);
  usersName.push(users[i].name);
  usersAge.push(users[i].age);;
}

var ul = document.createElement('ul');
div.appendChild(ul);
for (let i = 0; i < users.length; i++) {
  var li = document.createElement('li');
  li.innerHTML = [usersName[i], usersAge[i], usersEmails[i]];
  ul.appendChild(li);
}
&lt;div class="app"&gt;&lt;/div&gt;

【讨论】:

    【解决方案3】:

    您需要迭代两次才能实现目标。首先,您需要使用mapfor 关键字遍历数组,然后当您有一个对象时,您需要使用Object.keys 遍历该对象。像这样的:

    const users = [
    {
      name: "Helene",
      age: 54,
      email: "helene@helene.com",
    }, {
      name: "Janet",
      age: 24,
      email: "janet@janet.com",
    }, {
      name: "Michel",
      age: 25,
      email: "michel@michel.com",
    }];
    
    // first, you need to loop through the array to get each user and then iterate over the user itself for its properties
    
    
    users.map(user=> {
    
      Object.keys(user).map(key => {
          console.log(user[key])
          // we are printing the 'key' property of the user Object.
      }) 
          // the above is the one you want; you can push it to an array and then render it in your HTML file
    })

    【讨论】:

      【解决方案4】:

      您已经使事情变得更复杂了,您需要这样做。您需要一个div 和一个ul。当您遍历数据时,您可以在每次迭代时创建一个新的li,然后将用户数据附加到其文本内容中。然后附上liul,最后,在循环之后,附上ul到页面。

      const users=[{name:"Helene",age:54,email:"helene@helene.com"},{name:"Janet",age:24,email:"janet@janet.com"},{name:"Michel",age:25,email:"michel@michel.com"}];
      
      const div = document.querySelector('.app');
      const ul = document.createElement('ul');
      
      for (let i = 0; i < users.length; i++) {
        const user = users[i];
        const li = document.createElement('li');
        li.textContent = `${user.name}, ${user.age}, ${user.email}`;
        ul.appendChild(li);
      }
      
      div.appendChild(ul);
      &lt;div class="app"&gt;&lt;/div&gt;

      其他文档

      【讨论】:

        猜你喜欢
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 2014-12-22
        • 1970-01-01
        • 2017-01-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多