New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cloneDeep maintains reference after cloning? #2725
Comments
Hi @hulbert! Our y.one === y.two // true
yy.one === y.one // false
yy.one === yy.two // true |
@jdalton would you accept a PR to better document this behavior? Additionally, is there a way to deep clone without this behavior in lodash. The use case is a large object/document that may need to be mutated but has some duplicate values via reference to avoid repetition and clarify that they're the same. In our case, a large deeply nested settings object for configuring a job. |
The clone behavior is glossed over as "This method is loosely based on the structured clone algorithm". See http://w3c.github.io/html/infrastructure.html#section-structuredclonewithtransfer
You might be able to customize the cloning behavior with _.cloneDeepWith. |
I would also vote for it to be better documented that this is the case. Especially since many sources inadequately or incompletely explain lodash's clone:
incorrectly equate it with jQuery's deep extend:
or don't mention lodash at all, when it would be worth a footnote:
I think the only mention of this behavior that I've managed to find is this issue. Even the link to MDN in _.clone's description only says Just adding a minimal repro to the examples for _.clone and _.cloneDeep (like the one @hulbert included above) would probably save some headaches. |
Agreed, an answer to this:
would be welcome. 👍 |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Not sure if this is intended, it doesn't match what I use
_.cloneDeep
for but perhaps it's desired for other cases. It came up unexpectedly in our codebase so I'd be curious—if it's intended behavior—if it could be documented clearly.It seems that
_.cloneDeep
maintains, in the cloned object, two keys referencing the same object in the original object.Example case:
This prints:
I would have expected it to print:
The text was updated successfully, but these errors were encountered: