I have a question regarding shallow and deep copy of an object. I was giving an examination recently (PeopleCert) and up to that point i was led to believe that a shallow copy of an object is achieved either with the spread operator(...) or Object.assign and a deep copy with Json.Parse and stingify but when a question came up on how we create a deep copy about that there was no option for JsonParse but for Object.Assign. Now i have looked around the internet and in some places, they say that the spread operator and object assign is a shallow copy and in some other places that it is a deep copy. Which of those methods is a shallow and which is a deep copy? I know there is also Lowdash but it wasnt an option in the exams.
Asked
Active
Viewed 344 times
0
-
Problem is that in that place of the internet it will tell you that this is a shallow copy and in another it will tell you that its not. I am trying to find a definitive answer. – Arthur Wellesley Jun 06 '21 at 19:44
-
Both `Object.assign` (with a new object as the target) and an object literal with spread syntax make shallow copies. Of course you can nest them to make deep copies, maybe that's what the pages you found referred to, or they were simply wrong. – Bergi Jun 06 '21 at 19:49