You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi there. I had the same question as you when I was implementing something. There is a way to do this but it is not something directly provided by JavaScript but it worked for me. You can do the following:
let copiedObject = JSON.parse(JSON.stringify(object))
The stringify function first puts the object into a stringified json string and then parses it again, which deep copies the object. However, I warn you that this is an expensive function and can really slow down the program if used many times so use with caution. There are other ways to copy objects I believe and they are as follows:
Moreover, jQuery provides a way to deep copy too!:
let copiedObject = $.extend(true, {}, objectToBeCopied);
This is a really smart function because it deep copies the object to an empty object, which is the target, and returns the target. Specifying true deep copies the object recursively, which can be significantly faster. You can pass in further objects as arguments to merge those objects into the target empty object. What the function above is doing is that it merges objectToBeCopied into {} and returns the filled {} (the target). Further documentation can be found at https://api.jquery.com/jquery.extend/
Is there a way do deep copy an object?
Thanks.
The text was updated successfully, but these errors were encountered: