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

138
Vistas
Can a simple for loop in Javascript avoid mutation of array when copied

I have a code snippet as below

 let arr1 = [{status : true , name : "one"} , {status : false , name : "two"} , {status : true , name : "three"}]
    let arr2 = arr1.slice();

    for(let i=0; i<arr2.length ; i++){
      if(!arr2[i].status){
        arr2[i].status = true;
      }
    }

    console.log("arr1....." , arr1);
    console.log("arr2......",arr2)

In this case both arr1 and arr2 are modified. My expected result is just to update arr2 and not arr1 (only using a simple for loop)

How can I achieve this?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Objects inside arrays point same references, so if you update an object in arr2 it will update same object in arr1. So you have 2 options,

  1. Deep copy arr1 while assigning arr2 (deep copy means copy even objects inside, may run slower)

let arr1 = [{status : true , name : "one"} , {status : false , name : "two"} , {status : true , name : "three"}]
let arr2 = JSON.parse(JSON.stringify(arr1));

for(let i=0; i<arr2.length ; i++){
  if(!arr2[i].status){
    arr2[i].status = !arr2[i].status;
  }
}

console.log("arr1....." , arr1);
console.log("arr2......",arr2)

2. Copy object(you would like to change), add changed field as below

let arr1 = [{status : true , name : "one"} , {status : false , name : "two"} , {status : true , name : "three"}]
let arr2 = [...arr1];

for(let i=0; i<arr2.length ; i++){
  if(!arr2[i].status){
    arr2[i] = {...arr2[i], status:!arr2[i].status};
  }
}

console.log("arr1....." , arr1);
console.log("arr2......",arr2)

about 4 years ago · Juan Pablo Isaza Denunciar

0

How about change a way to copy the array?


To explain why slice() will make both of them updated, that is because because the array.slice() also copy the reference of the orginial array.

So, whenever the original array changed, the copied array also changed

let arr1 = [{status : true , name : "one"} , {status : false , name : "two"} , {status : true , name : "three"}]
    const arr2 = JSON.parse(JSON.stringify(arr1));

    for(let i=0; i<arr2.length ; i++){
      if(!arr2[i].status){
        arr2[i].status = true;
      }
    }

    console.log("arr1....." , arr1);
    console.log("arr2......",arr2)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You are creating a new array but the objects inside it are still referencing the original ones. You need to create new references for these objects. Use the following instead of let arr2 = arr1.slice(); This would be the "Deep Copy" approach that @Tuba Polat mentioned.

let arr2 = arr1.map(oldObj => { return { ...oldObj } })
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