Quick Javascript deep copy
Recently, I had to implement a clone button at work.
Unfortunately, the copied item had a attribute items
that was a list.
Whenever the cloned item changed or the original item changed, they
both were updating. This is common problem in both Python and JS.
above - reference issue with cloned items
Unfortunately, underscore.js
only has a shallow copy _.clone
The solution is easier than I thought it was going to be:
var copiedItems = JSON.parse(JSON.stringify(items));
That’s it! A hacky well supported way to deep copy.