mkkowg
Last Updated: February 25, 2016
·
7.223K
· sschepis
7936772fc9628ea01635100e62fe2015

Deep clone a javascript object without underscore or lodash

It's ridiculously simple:

var clone = JSON.parse(JSON.stringify(obj)); 

The only time this won't work is with objects with circular dependencies, which make JSON.stringify throw an error. Still, this is often a great solution in projects that require a deep clone but don't warrant packaging an entire library like lodash or underscore to do it.

Say Thanks
Respond

3 Responses
Add your response

17341
482d2167f653570c6574b5a0de156a28

You made my day ;)

over 1 year ago ·
17880

That's great! haha

over 1 year ago ·
21290
None

This is a clever solution, however it doesn't copy keys with assigned functions as their values.

over 1 year ago ·