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

145
Vistas
Javascript: Reassigned variable being mutated

Any idea why my variable assignment is being mutated? This seems like something simple but I'm just not sure what's going on.

Below, an object of data is passed into the pullChecklist function as a prop. I want to take this data, create a copy (newPage), change the ids in that copy and then save the copy in state.

However, whenever I try and change the ids in the copy newPage, the ids also change in page. This is confirmed by the 3 console logs.

If I remove the if statement all together and just leave the Original Assignment and Original Assignment 2 console logs, the data is exactly as when it is sent as a prop. Adding the if statement mutates all the console logs to the version with updated ids.

const pullChecklist = (page) => {
    console.log('Original Assignment', page.components[0].blocks)
    let newPage = page
    if (newPage.components[0].blocks) {

      newPage.components[0].blocks.map((block) => {
        if (block.type === 'checklist') {
        
          let newTest = uuidv4()
          block.id = newTest

          for (const c in block.data) {
            let newId = uuidv4()
            block.data[c].id = newId
          }
          console.log('Reformatted', block);
        }
      });
    }
    console.log('Original Assignment 2', page.components[0].blocks);

  };

Why is the data from page being mutated? Thank you!

EDIT:

As pointed out by @Vlaz below, let newPage = page does not copy the object.

I used: obj = JSON.parse(JSON.stringify(o)); in order to properly copy the object.

about 4 years ago · Juan Pablo Isaza
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