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. 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. Published on Feb 21, 2019 Tags: JavaScript
| clone
| JSON.parse
Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article
that you just finished reading.
No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner
or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in
and improve your skillset with any of the tutorials below.
Using JSON.parse and JSON.stringify to clone
Related Posts
Tutorials
Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.