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

178
Visualizações
Shouldn't using spread syntax not reference the original array?

I have an Object array in my state called names, i'm trying to make a copy of this array to another Object array in state called shuffledNames, but when i shuffle the shuffledNames using the code below, the original names changes the ids to match the new shuffled ids. See images below for an example. I've tryed using .concat(), .slice(). I do not know what to try anymore.

handleSort = () => {
  let tempName = [...this.state.names];

  let shuffledNames = tempName;

  for (let i = shuffledNames.length; i-- > 1; ) {
    let j = Math.floor(Math.random() * i);
    let temp = shuffledNames[i];
    shuffledNames[i] = shuffledNames[j];
    shuffledNames[j] = temp;
  }

  shuffledNames.forEach((shuffledName, index) => {
    shuffledName.id = index;
  });

  this.setState({ shuffledNames: shuffledNames });
};

This is my state before shuffling

This is my state before shuffling

This is my state after shuffling

This is my state after shuffling

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The spread operator is a shallow clone, so it's a new array but the same values which are object references, not new objects. You need to find or code your own clone or deep copy function. Last one I used was randa's clone function but that was because someone else on my team had already imported that library.

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