Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我有一个对象数组,但如何单独显示它们_Javascript - Fatal编程技术网

Javascript 我有一个对象数组,但如何单独显示它们

Javascript 我有一个对象数组,但如何单独显示它们,javascript,Javascript,我的本地存储具有以下功能: 注册表项 价值观-- [{ "firstname": "lily", "lastname": "ong", "NRIC": "S1234567Z", "DOB": "2018-07-30", "owncontact": "12345678", "homecontact": "12345678", "email": "123@gmail.com", "blk": "123", "street": "12345", "houseno

我的本地存储具有以下功能:

注册表项

价值观--

[{
  "firstname": "lily",
  "lastname": "ong",
  "NRIC": "S1234567Z",
  "DOB": "2018-07-30",
  "owncontact": "12345678",
  "homecontact": "12345678",
  "email": "123@gmail.com",
  "blk": "123",
  "street": "12345",
  "houseno": "123",
  "postalcode": "123456",
  "preferredevent": "wcp"
}, {
  "firstname": "Mickey",
  "lastname": "Mouse",
  "NRIC": "S1234567Z",
  "DOB": "2018-08-29",
  "owncontact": "12345678",
  "homecontact": "12345678",
  "email": "345@gmail.com",
  "blk": "123",
  "street": "12345",
  "houseno": "123",
  "postalcode": "123456",
  "preferredevent": "cb"
}]
我想在我的身体里展示一下:

<p id="title">List of users who has registered for the event:</p>
<br/><br/>
<ol>
    <li id="listofusers" ></li>
</ol>

您需要做的是将id放入OL对象中。 然后编写一个循环来解析对象。在本例中,我提取了localstorage值并将其放入clients变量中。您可以将其复制并粘贴到html页面中,以查看其运行情况

<ol id="listofusersusingindex">
</ol>
<ol id="listofusersusingin">
</ol>
    <script>
    var clients = [{
      "firstname": "lily",
      "lastname": "ong",
      "NRIC": "S1234567Z",
      "DOB": "2018-07-30",
      "owncontact": "12345678",
      "homecontact": "12345678",
      "email": "123@gmail.com",
      "blk": "123",
      "street": "12345",
      "houseno": "123",
      "postalcode": "123456",
      "preferredevent": "wcp"
    }, {
      "firstname": "Mickey",
      "lastname": "Mouse",
      "NRIC": "S1234567Z",
      "DOB": "2018-08-29",
      "owncontact": "12345678",
      "homecontact": "12345678",
      "email": "345@gmail.com",
      "blk": "123",
      "street": "12345",
      "houseno": "123",
      "postalcode": "123456",
      "preferredevent": "cb"
    }];
在培训中,我介绍了两种通过JavaScript和HTML获取数据的方法

首先,您将注意到必须获取html元素。让我们用JavaScript来实现这一点。其次,循环需要知道元素的数量,这样才能获得长度。然后循环遍历引用其属性的对象,并将+=附加到HTML对象中

    var listofusersusingindex = document.getElementById("listofusersusingindex");
    var count = clients.length;
    for(var i = 0; i < count; i++)
    {
        listofusersusingindex.innerHTML += "<li>" + clients[i].firstname + " " + clients[i].lastname + "</li>";
    }

    var listofusersusingin = document.getElementById("listofusersusingin");
    for(var item in clients)
    {
        listofusersusingin.innerHTML += "<li>" + clients[item].firstname + " " + clients[item].lastname + "</li>";
    }

    </script>

你希望有人为你写循环,你的问题是什么?在数组上动态生成html循环?