Each blocks

If you need to loop over lists of data, use an each block:

  1. <ul>
  2. {#each cats as cat}
  3. <li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">
  4. {cat.name}
  5. </a></li>
  6. {/each}
  7. </ul>

The expression (cats, in this case) can be any array or array-like object (i.e. it has a length property). You can loop over generic iterables with each [...iterable].

You can get the current index as a second argument, like so:

  1. {#each cats as cat, i}
  2. <li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">
  3. {i + 1}: {cat.name}
  4. </a></li>
  5. {/each}

If you prefer, you can use destructuring — each cats as { id, name } — and replace cat.id and cat.name with id and name.