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
JavaScript
x
12
12
1
if(localStorage.getItem(saveName) == null){
2
var nonLoopScenes = [scenes];
3
nonLoopScenes.forEach(element=>{
4
element.objects.forEach(element2=>{
5
element2.scene = null;
6
});
7
});
8
console.log(nonLoopScenes);
9
console.log(scenes);
10
localStorage.setItem(saveName,JSON.stringify(scenes));
11
}
12
Advertisement
Answer
You can also try const arrayCopy = JSON.parse(JSON.stringify(initialArray))
, although the method using the spread operator should work as well.