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: 
Right now there's no way to do this another way than what you propose (the `each` hack or always doing `comments[commentId]`). There's a incoming new feature for Svelte though which will make this possible: [Using @const in markup](https://github.com/sveltejs/rfcs/blob/43e386370e8d122e7adc0f3b60c41672bfab5410/text/0000-markup-constants.md). This is not implemented yet, but when it is, you can do this:

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

```
Are you looking to do something like [this](https://svelte.dev/repl/42f3fa98746c4f3eac0b8e867f60f59b?version=3.38.3)?

```html
<script>
  let commentsSortedByDate = ["id2", "id1"];
  let comments = {
    id1: {
      text: "lorem",
    },
    id2: {
      text: "ipsum",
    },
  };
</script>

<h2>
    Comments
</h2>
{#each commentsSortedByDate as comment}
  <ul>
    <li>
            {comment}: {Object.values(comments[comment])}
    </li>
  </ul>
{/each}

```