I have created a copy of a variable using Object.assign and I am able to change an array within the copy of the object fine without affecting the original object. However, if I try and update a value within an array on the copied object this is also affecting the original object (i.e. the changes are made to both "job" and "updatedJob").
I have the following code:
// Create copy of "job" object which has been passed through
let updatedJob = Object.assign({}, job);
// Remove images from updated job object
updatedJob.Images = null;
// Remove checklist images from updated job object
_.each(updatedJob.Checklists, function (checklist) { checklist.Image = null; });
As you can see I have created a copy of the "job" object that has been passed through.
If I then change the "Images" array of the "updateJob" object (to null) this works fine and does not affect the original "job" object (i.e. the original "job" object still has images intact).
However in the last line of the code I am trying to iterate the "Checklists" array witin the "updatedJob" object and change just the "Image" property to "null" for each one but doing this also updates the original "job" object.
How is this possible when the "updatedJob" object should be a copy of the original "job" object therefore any changes I make to it should not affect the original "job" object?