0

im trying to make a copy of an array but when i change the copy it also changes the original. i have tried using Object.assign([], scenes), scenes.clone() and tried using a for loop

if(localStorage.getItem(saveName) == null){
    var nonLoopScenes = [...scenes];
    nonLoopScenes.forEach(element=>{
        element.objects.forEach(element2=>{
            element2.scene = null;
        });
    });
    console.log(nonLoopScenes);
    console.log(scenes);
    localStorage.setItem(saveName,JSON.stringify(scenes));
}
t_ perm
  • 3
  • 2

1 Answers1

0

You can also try const arrayCopy = JSON.parse(JSON.stringify(initialArray)), although the method using the spread operator should work as well.

  • i can't do that because the scenes array has a reference to it self multiple places. exaple:` var a = {name:"Oliver"}; var b = {name:"Noah", friend:a}; a.friend = b; ` – t_ perm Mar 11 '21 at 12:40