Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!