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

81
Views
¿Elemento de matriz que usa su propio índice de objeto principal como su valor?

digamos que tenemos:

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

Pregunta real en los comentarios del código ... lo que estoy tratando de hacer es completar las entradas de un formulario usando un objeto, y quiero poder mostrar el índice real del objeto dentro de la matriz en una de las entradas

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

0

en lugar de usar el índice como identificación, sugiero crear una identificación compleja o puede usar la biblioteca uuid() para crear una identificación de usuario única 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

Simplemente inicialice la matriz sin la identificación y agréguela luego usando un bucle for.

 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!