Follow @endyourif rss Facebook LinkedIn

Deep clone an object with JavaScript Deep clone an object with JavaScript

Published on Feb 21, 2019

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.

Tags: JavaScript | clone | JSON.parse

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP