Deep clone an object with JavaScript

D

To copy/clone an object using JavaScript, you will want to leverage the JSON.parse function in combination with the JSON.stringify function.

If you are looking for a jQuery solution, you will want to leverage the extend function which can be found in the Deep clone an object with jQuery article.

Using JSON.parse and JSON.stringify to clone

In the following example two objects will be created: objectA and objectB. objectA will be used as the existing object that will be copied/cloned into objectB.

Once objectA has been cloned to objectB using the JSON.parse function in combination with the JSON.stringify function, the output of the copy is outputted on screen and shows that the full object, including child properties, have been copied to the new object.

About the author

By Jamie

My Books