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

77
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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