Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

80
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda