1

I have an object

{
  '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' ]
  ]
}

How would I access the information within the array within the array?

I have tried:

<% 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++ %>
    <% } %>
<% } %>

But value just outputs the string index for the name stored in key.

When I try

<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++ %>
    <% } %>
<% } %>

It just outputs the names over and over again in a table.

1
  • key is the key from your object, but you need the array, so use timesheets[key] Commented Apr 3, 2020 at 21:28

2 Answers 2

3

Try this:

<% 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++ %>
    <% } %>
<% } %>

Note that key is each key of timesheets so timesheets[key] will be the value(array of arrays) of that key.

Sign up to request clarification or add additional context in comments.

Comments

1

try something like this:

        <% for(var entry of timesheets[key]){ %>
            <td> Date: <%= entry[0] %> </td>
            <td> Time: <%= entry[1] %> </td>
        <% } %>

1 Comment

the inner loop should be for...of not for...in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.