I want to copy a global static array to individualise the array to each user session.
I tryed to copy that array with concat/slice/[...array] but it takes every time the same reference / pointer. Only with JSON.parse(JSON.stringify(array)) it seems to work.
Is there a more efficient way to copy a array / object / variable without geting the reference / pointer with it
var Array2 = [...Array];
var Array2 = Array.concat();
var Array2 = Array.slice();
dosent work.
var Array = [{
test: 'i am a test'
}]
var Array2 = Array;
Array2.favorite = true;
console.log(Array) //result: test: 'i am a test', favorite: true
var Array3 = JSON.parse(JSON.stringify(Array));
console.log(Array) //result: test: 'i am a test'
JSON.parse(JSON.stringify(Array)). What are your concerns with that method? Realistically, if your concerns are with performance, you shouldn't be performing regular deep copies of objects period.