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

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

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