In the screenshot provided, I have the car
object (unable to pretty print it here).
https://i.sstatic.net/63Y36.png
I am looking to create a function that clones one of the two elements in the factory
.
My attempt at this function was as follows:
public cloneFactory(modelIndex: number, factoryIndex: number): void {
const newFactory = this.car.model[modelIndex].factory.slice(factoryIndex, factoryIndex + 1);
this.car.model[modelIndex].factory.push(newFactory[0]);
}
I also tried the traditional method:
public cloneFactory(modelIndex: number, factoryIndex: number): void {
const newFactory = this.car.model[modelIndex].factory[factoryIndex];
this.car.model[modelIndex].factory.push(newFactory);
}
The issue I am facing is that any changes made to the cloned object affect the original object as well. I cannot comprehend why the original and the clone remain linked after executing either of the methods mentioned above.
What is the correct approach to cloning an element of the array so that edits can be made later without affecting the original object?