访问对象内数组中数组的值

问题描述 投票:1回答:2

我有一个对象

{
  'Bob Joerson': [
    [ 'Tuesday March 31, 2020', '07:58:12.0' ],
    [ 'Wednesday April 1, 2020', '11:00:03.7' ]
  ],
  'Joe Bobberson': [
    [ 'Tuesday March 31, 2020', '07:58:12.0' ],
    [ 'Wednesday April 1, 2020', '11:00:03.7' ]
  ]
}

我将如何访问阵列内阵列中的信息?

我尝试过:

<% for(var key in timesheets){ %>
    <% if(timesheets.hasOwnProperty(key)){ %>
    <% a = 0 %>
<table id="timesheetTable" class='table-primary table-bordered table' style='border-spacing: 10px;'>
    <tr>
        <td rowspan="2" id="name"> <%= key %> </td>
        <% for(var value in key){ %>
            <td> <%= value %> </td>
        <% } %>
    </tr>
</table>
    <% a++ %>
    <% } %>
<% } %>

但是值仅输出键中存储的名称的字符串索引。

当我尝试

<h1>Timesheet for dates</h1> 

<% for(var key in timesheets){ %>
    <% if(timesheets.hasOwnProperty(key)){ %>
    <% a = 0 %>
<table id="timesheetTable" class='table-primary table-bordered table' style='border-spacing: 10px;'>
    <tr>
        <td rowspan="2" id="name"> <%= key %> </td>
        <% for(i = 0; i < key.length; i++){ %>
            <td> <%= key %> </td>
        <% } %>
    </tr>
</table>
    <% a++ %>
    <% } %>
<% } %>

它只是在表中一遍又一遍地输出名称。

javascript ejs
2个回答
1
投票

尝试一下:

<% for(var key in timesheets){ %>
    <% if(timesheets.hasOwnProperty(key)){ %>
    <% a = 0 %>
<table id="timesheetTable" class='table-primary table-bordered table' style='border-spacing: 10px;'>
    <tr>
        <td rowspan="2" id="name"> <%= key %> </td>
        <% for(var value of timesheets[key]){ %>
            <td> <%= value[0] %><%= value[1] %> </td>
        <% } %>
    </tr>
</table>
    <% a++ %>
    <% } %>
<% } %>

请注意,keykey的每个timesheets,因此timesheets[key]将是该键的值(数组的数组)。


0
投票

尝试这样的事情:

        <% for(var entry in timesheets[key]){ %>
            <td> Date: <%= entry[0] %> </td>
            <td> Time: <%= entry[1] %> </td>
        <% } %>
© www.soinside.com 2019 - 2024. All rights reserved.