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

76
Views
Array item using its own parent object index as its value?

lets say we have :

    let array_of_objects = [
    {
      "color": "purple",
      "type": "minivan",
      "id": this.object.index // i know this is not valid code,but can this somehow get its own index within the array as value ?

    },
    {
      "color": "red",
      "type": "station wagon",
      "id": this.object.index  //this should be 1
    }
]

Actual question in code comments.. what i'm trying to do is to fill a form's inputs using an object,and i want to be able to display the actual index of the object within the array in one of the inputs

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

instead of using index as id I suggest create a complex id or you can use uuid() library for creating unique user id uuid npm

import { v4 as uuidv4 } from 'uuid';
array_of_objects = array_of_objects.map((item, index) => ({ id: uuidv4(), ...item}))

console.log(array_of_objects);
about 4 years ago · Juan Pablo Isaza Report

0

   let array_of_objects = [
    {
      "color": "purple",
      "type": "minivan"

    },
    {
      "color": "red",
      "type": "station wagon"
    }
];

array_of_objects = array_of_objects.map((item, index) => ({ id: index, ...item}))

console.log(array_of_objects);

about 4 years ago · Juan Pablo Isaza Report

0

Just initialize the array without the id and add it afterwards using a for loop.

  let array_of_objects = [
  {
    "color": "purple",
    "type": "minivan",
  },
  {
    "color": "red",
    "type": "station wagon",
  }
]
for (let i = 0; i < array_of_objects.length; i++) {
  array_of_objects[i].id = i;
}
about 4 years ago · Juan Pablo Isaza 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!