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

199
Visualizações
Updating an objects nested property in a for loop

I've looked around a good amount on stackoverflow and haven't found this question yet. I feel like I'm missing something obvious so it's possible the question exists and I couldn't find it. My issue is that I'm trying to create an array of objects with different id's. When I run the for loop and update the id's based on the value of i, the resulting array has id's that are all the same.

Here's a sample snippet -

const baseObj = {
    nestedId: {
      id: 1,
  }
}

const objArr = [];
for(let i=0; i<3; i++) {
    const newObj = {...baseObj}
    newObj.nestedId.id = i + 1;
    objArr.push(newObj)
}

The resulting array is full of objects with the same id

{
  id: 3
}

I'm unsure what to do so I can make each id be different. Any help greatly appreciated.

about 4 years ago · Santiago Gelvez
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