Question: I have an object `comments`:

`let comments = {"id1": ..., "id2: ..., ...};`

I then have an array `commentsSortedByDate`:

`let commentsSortedByDate = ["id2", "id1", ...];`

I would like to iterate over these comments. This is what I've tried:

```
{#each commentsSortedByDate as commentId}
    <script> let comment = comments[commentId]; </script>
    <div class="comment" class:new={comment.isNew}> ... </div>
{/each}

```

This does not work. How can I define this `comment` variable in the loop so that I can access it multiple times without having to type `comments[commentId]` each time?

Answer: 
Or you could simply remap your input array on the fly and iterate over `comment` objects rather than ids:

```
{#each commentsSortedByDate.map(id => comments[id]) as comment}
    <div class="comment" class:new={comment.isNew}> ... </div>
{/each}

```
I have come up with a hackish solution, but I will not be accepting this as a proper answer.

```
{#each commentsSortedByDate as commentId}
    {#each [comments[commentId]] as comment}
        <div class="comment" class:new={comment.isNew}> ... </div>
    {/each}
{/each}

```