Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
How to get a unique index for nested loops in Svelte Kit

I'm creating a website for uploading images. You can create albums, and each album can contain a number of posts. There are numerous photos in each post (a array of images).

It looks like the following:

const albums = [
    {
        id: 1,
        title: 'Album 1',
    }
]

const posts = [
    {
        "album": 1,
        "images": [
            "https://picsum.photos/200/300",
            "https://picsum.photos/200/300",
        ],
        "author": 1,
    },
    {
        "album": 1,
        "images": [
            "https://picsum.photos/200/300",
        ],
        "author": 2,
    },
    {
        "album": 1,
        "images": [
            "https://picsum.photos/200/300",
            "https://picsum.photos/200/300",
            "https://picsum.photos/200/300",
            "https://picsum.photos/200/300",
            "https://picsum.photos/200/300",
        ],
        "author": 4,
    }
]

For each post image in the array, I need to obtain a distinct index number. How I'm trying to obtain it in Svelte is as following:

{#each posts as post}
    {#each post.images as image, index}
        {index}: {image}
    {/each}
{/each}

However, this won't give me an unique index for every image. It will result in the following:

0: https://picsum.photos/200/300
1: https://picsum.photos/200/300
0: https://picsum.photos/200/300
0: https://picsum.photos/200/300
1: https://picsum.photos/200/300
2: https://picsum.photos/200/300
3: https://picsum.photos/200/300
4: https://picsum.photos/200/300

Except, I need it like this:

0: https://picsum.photos/200/300
1: https://picsum.photos/200/300
2: https://picsum.photos/200/300
3: https://picsum.photos/200/300
4: https://picsum.photos/200/300
5: https://picsum.photos/200/300
6: https://picsum.photos/200/300
7: https://picsum.photos/200/300

How would I be possible to achieve the above one?

One thought of mine would be reducing/mapping the array and creating a post object for each single image. However, I'm unsure how to achieve this.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can create a "getter" function to get all the images from every post

function getImages() {
    return posts.map(post => post.images).flat()
}

And use that instead of two nested each loops

{#each getImages() as image, index}
    {index}: {image}
{/each}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Generate a nested array containing the desired numbers:

<script>
  // const posts = ...

  $: numbers = buildNumbers(posts)

  function buildNumbers(posts) {
    let nr = 1
    const result = []
    posts.forEach((post, postIndex) => {
      post.images.forEach((image, imageIndex) => {
        result[postIndex] = result[postIndex] || []
        result[postIndex][imageIndex] = nr
        nr++
      })
    })
    return result
  }
</script>

<ul>
{#each posts as post, postIndex}
  {#each post.images as image, imageIndex}
    <li>{numbers[postIndex][imageIndex]}: {image}</li>
  {/each}
{/each}
</ul>

REPL

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda