In my code I want to save an object that changes during the execution in an array inorder to use it after the execution of the function,so I tried to create a copy of it using this approach:
let copy=[...original]//original is the variable that changes during the execution
when I used this approach I noticed that the content of the copied array is the same as the last value of the original array.
Now I need a method that can keep all the states of the original array in one variable.
You can give a try to structuredClone() method which help you in creating a deep clone of a given value using the structured clone algorithm.
// Create an object with a value
const original = { name: "Alpha" };
// Clone it
const clone = structuredClone(original);
// Change original object value in execution
original.name = 'Beta';
console.log(original); // { "name": "Beta" }
console.log(clone); // { "name": "Alpha" }
console.log(clone !== original); // true