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

104
Vistas
How do I use map function, so I can add the index as a property to the newly constructed object?

I have an array of indexes: selected = [2, 6, 10] () for instance. I also have array of photo objects called photos. I want to keep track of only selected photos so I do :

const selectedPhotos = selectedArray.map((i) => photos[i]); 

Which gives me the result:

Array [
  Object {
    "albumId": "someId1",
    "creationTime": "sometime",
    "duration": 0,
    "filename": "name1.jpg",
    "id": "someId1",
    "uri": "someIUri1",
  },
  Object {
    "albumId": "someId2",
    "creationTime": "sometime",
    "duration": 0,
    "filename": "name2.jpg",
    "id": "id2",
    "uri": "uri2",
  },
  Object {
    "albumId": "someId3",
    "creationTime": "sometime",
    "filename": "filename3.jpg",
    "uri": "uri3",
  },
 ]

Which is good, BUT I also want to keep track of the selected index. So my question is, how should I add something like the following information: index: 2, or index: 6, etc alongside the photo properties. So I want to return something like:

Array [
      Object {
        "index": 2,
        "albumId": "someId1",
        "creationTime": "sometime",
        "duration": 0,
        "filename": "name1.jpg",
        "id": "someId1",
        "uri": "someIUri1",
      },
      Object {
        "index": 6
        "albumId": "someId2",
        "creationTime": "sometime",
        "duration": 0,
        "filename": "name2.jpg",
        "id": "id2",
        "uri": "uri2",
      },
      Object {
        "index": 10
        "albumId": "someId3",
        "creationTime": "sometime",
        "filename": "filename3.jpg",
        "uri": "uri3",
      },
     ]
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

you can do something like this

const selectedPhotos = selectedArray.map((i) => {
 let curItem={...photos[i]};
 curItem.index=i;
  return curItem;
}); 
about 4 years ago · Santiago Trujillo Denunciar

0

Simply add the index:

const selectedPhotos = selectedArray.map((i) => {
  let photo = photos[i];
  photo.index = i;
  return photo;
}); 
about 4 years ago · Santiago Trujillo Denunciar

0

You can create a new object and destructure the one you want inside of it, then add the index. That way you can do it in one line and it's easy to read.

const selectedPhotos = selectedArray.map(i => ({ ...photos[i], index: i })); 

Safer version with optional chaning:

const selectedPhotos = selectedArray.map(i => ({ ...photos?.[i], index: i })); 
about 4 years ago · Santiago Trujillo 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