{"id":4965,"date":"2024-10-09T07:21:36","date_gmt":"2024-10-09T07:21:36","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=4965"},"modified":"2024-10-09T07:21:39","modified_gmt":"2024-10-09T07:21:39","slug":"how-to-clone-object-in-javascript","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/","title":{"rendered":"How to Clone Object in JavaScript"},"content":{"rendered":"\n<p>JavaScript objects are key-value pairs that allow you to store complex data in a compact manner. They are commonly used by today&#8217;s web developers to store various information such as user details, states, etc. since they can be easily converted into JSON and vice versa. Often, web developers need to copy a JavaScript object for some reason or the other. It can be tricky to clone object in JavaScript since you cannot use equality &#8216;=&#8217; operator for this purpose, the way you do it with plain variables. In this article, we will learn several different ways to clone JavaScript objects.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#Why_Clone_JavaScript_Object\" >Why Clone JavaScript Object<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#Shallow_Clone_vs_Deep_Clone\" >Shallow Clone vs Deep Clone<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#Shallow_Cloning\" >Shallow Cloning<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#Deep_Cloning\" >Deep Cloning<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#How_to_Clone_Object_in_JavaScript\" >How to Clone Object in JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#1_Using_Spread_Operator\" >1. Using Spread Operator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#2_Using_ObjectAssign\" >2. Using Object.Assign<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#3_Using_JSON\" >3. Using JSON<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#4_Using_Loops\" >4. Using Loops<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Clone_JavaScript_Object\"><\/span>Why Clone JavaScript Object<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>JavaScript objects are collections of key-value pairs where each value can be a simple data such as strings or integers, or complex data such as another JS object itself. Here is an example of a JavaScript Object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var user = {<br>   name: 'John',<br>   age: 15,<br>   city: 'NYC'<br>   }<\/pre>\n\n\n\n<p>If you want to clone JavaScript object, people most likely use an equality operator for it. Let us see what happens when you do this.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var new_user=user;<br>console.log(new_user); \/\/ {name: 'John', age: 15, city: 'NYC'}<\/pre>\n\n\n\n<p>So far so good. Now let us update the name key&#8217;s value in new object. Then we display the content of both user and new_user objects.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user.name='Jane';<br>console.log(new_user); \/\/ {<strong>name: 'Jane'<\/strong>, age: 15, city: 'NYC'}<br>console.log(user);  \/\/ {<strong>name: 'Jane'<\/strong>, age: 15, city: 'NYC'}<\/pre>\n\n\n\n<p>You will find that although you changed the <em>name<\/em> key&#8217;s value of only new_user object, it has also changed the corresponding value in user object. How did this happen? This is because the new_user is not a separate copy of user object but simply a new reference to it. So new_user and user point to the same object.<\/p>\n\n\n\n<p>But we want new_user to be a different object such that when we update it, those changes must not affect user object. This is why equality operator does not work when you are trying to clone JavaScript objects. This is the problem we will solve in this article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Shallow_Clone_vs_Deep_Clone\"><\/span>Shallow Clone vs Deep Clone<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before we learn the different ways to copy an object in JavaScript, it is essential to understand that there are two types of cloning possible in JavaScript &#8211; shallow and deep.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Shallow_Cloning\"><\/span>Shallow Cloning<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this type of cloning, the cloned object is a separate object, different from the original one. It has a different reference. However, if any of its properties are themselves JavaScript objects, then they will continue to reference those properties in the original object. In other words, only the top level properties are clones but the inner objects are the same for both objects. So if you make changes to any of the top-level properties in cloned object, they will not affect the original object. But if you make changes to any of the properties that are JS objects, they will affect those properties in original object also. Confusing? Don&#8217;t worry. We will see this with detailed examples below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Deep_Cloning\"><\/span>Deep Cloning<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this case, the cloned object is entirely different from the original one, with its properties as well as inner objects. Both of them are independent. So if you make changes to one of them, it will not affect the other one in any way. We will see this with the examples below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Clone_Object_in_JavaScript\"><\/span>How to Clone Object in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are mainly 4 different ways to clone objects in JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_Spread_Operator\"><\/span>1. Using Spread Operator<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <a href=\"https:\/\/www.w3schools.com\/react\/react_es6_spread.asp\">spread operator<\/a> (&#8230;) is one of the easiest ways to clone objects in JavaScript. It creates a new object reference. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cloned_object = {...original_object}<\/pre>\n\n\n\n<p>Here is an example to use spread operator. Let us say you want to clone the following JS object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var user = {<br>   name: 'John',<br>   age: 15,<br>   city: 'NYC',<br>   marks:{english:50,maths:49}<br>   }<\/pre>\n\n\n\n<p>Here is how to clone above object with spread operator and display the values of cloned object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user = {...user};<br>console.log(new_user); \/\/ { name: 'John', age: 15, city: 'NYC',marks:{english:50,maths:49} }<\/pre>\n\n\n\n<p>You will see that the cloned object contains same properties as the original object. But, please note, it is only a shallow copy and not a deep copy.<\/p>\n\n\n\n<p>Let us change the name property of new object and see if it changes the name property in original object also.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user.name='Jane';<br>console.log(new_user); \/\/ { name: '<strong>Jane<\/strong>', age: 15, city: 'NYC',marks:{english:50,maths:49} }<br>console.log(user);     \/\/ { name: '<strong>John<\/strong>', age: 15, city: 'NYC',marks:{english:50,maths:49} }<\/pre>\n\n\n\n<p>As you can see, the changes made to one of the non-object properties in cloned object do not affect the original object. <\/p>\n\n\n\n<p>But if you change the inner object marks, it will also affect the original object. Let us change the value of english property of marks object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user.marks.english=45<br><br>console.log(new_user);<br>console.log(user);<\/pre>\n\n\n\n<p>You will see the following output indicating that the marks.english value has changed in both objects.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{<br>  name: 'Jane',<br>  age: 15,<br>  city: 'NYC',<br>  marks: { <strong>english: 45<\/strong>, maths: 49 }<br>}<br>{<br>  name: 'John',<br>  age: 15,<br>  city: 'NYC',<br>  marks: { <strong>english: 45<\/strong>, maths: 49 }<br>}<br><\/pre>\n\n\n\n<p>Spread operator can also be used to merge two or more arrays and objects, as shown below. Here is an example to combine two objects into a new object, and to combine two arrays into a new array.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">object2 = {...object1, ...object2};<br>array3 = {...array1, ...array2};<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_ObjectAssign\"><\/span>2. Using Object.Assign<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also create a shallow copy of object using Object.assign() function. You can input two or more objects as input to this function and it will return a new object which is a combination of properties of input objects. <\/p>\n\n\n\n<p>Here is the syntax to clone an object using assign() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cloned_object = Object.assign({}, original_object);<\/pre>\n\n\n\n<p>Here is an example to clone user object using Object.assign() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var user = {<br>   name: 'John',<br>   age: 15,<br>   city: 'NYC',<br>   marks:{english:50,maths:49}<br>   };<br>   <br>new_user = Object.assign({},user);<br>console.log(new_user); \/\/ { name: 'John', age: 15,  city: 'NYC',  marks: { english: 50, maths: 49 }}<\/pre>\n\n\n\n<p>In this case also, you get a shallow copy. If you change the name property of new_user, it will not affect the original object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user.name='Jane';<br>console.log(user); \/\/ { <strong>name: 'John'<\/strong>, age: 15,  city: 'NYC',  marks: { english: 50, maths: 49 }}<br>console.log(new_user); \/\/ { <strong>name: 'Jane'<\/strong>, age: 15,  city: 'NYC',  marks: { english: 50, maths: 49 }}<\/pre>\n\n\n\n<p>But if you try to change the properties of inner object, they will impact both the cloned object as well as original object. This is because the inner object <em>marks<\/em> of new_user object still points to <em>marks<\/em> in original object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user.marks.english=45<br><br>console.log(new_user); \/\/ { name: 'John', age: 15,  city: 'NYC',  marks: { english: 45, maths: 49 }}<br>console.log(user); \/\/ { name: 'John', age: 15,  city: 'NYC',  marks: { english: 45, maths: 49 }}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_JSON\"><\/span>3. Using JSON<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Both the above solutions give you a shallow copy. If you want to completely create a separate copy of JavaScript object, then you need to use a combination of JSON.parse() and JSON.stringify() functions. It will create a deep copy of your object where each property has a separate existence. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cloned_object = JSON.parse(JSON.stringify(original_object))<\/pre>\n\n\n\n<p>In this case, first we need to use JSON.stringify() function to convert the JavaScript object to JSON string. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">json_user = JSON.stringify(user);<br>console.log(json_user); \/\/{\"name\":\"John\",\"age\":15,\"city\":\"NYC\",\"marks\":{\"english\":50,\"maths\":49}}<\/pre>\n\n\n\n<p>Then we call JSON.parse() on this JSON string to create a JavaScript object from it. In this case, you will get a new object completely different from the original one.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user = JSON.parse(json_user);<br>console.log(new_user); \/\/  { name: 'John', age: 15,  city: 'NYC',  marks: { english: 45, maths: 49 }}<\/pre>\n\n\n\n<p>You can combine both JSON.stringify() and JSON.parse() functions into a single statement as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user = JSON.parse(JSON.stringify(user));<\/pre>\n\n\n\n<p>Let us try modifying the name property of new_user and see if it changes original object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user.name='Jane';<br>console.log(new_user); \/\/  { <strong>name: 'Jane'<\/strong>, age: 15,  city: 'NYC',  marks: { english: 45, maths: 49 }}<br>console.log(user); \/\/  { <strong>name: 'John'<\/strong>, age: 15,  city: 'NYC',  marks: { english: 45, maths: 49 }}<\/pre>\n\n\n\n<p>You will see that the original object remains unchanged. Now let us change the inner property of cloned object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user.marks.english= 45;<br>console.log(new_user);<br>console.log(user);<\/pre>\n\n\n\n<p>You will see the following output indicating that the original object remains unchanged. This proves that new_user is a deep copy of user object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{<br>  name: 'John',<br>  age: 15,<br>  city: 'NYC',<br>  marks: { <strong>english: 45<\/strong>, maths: 49 }<br>}<br>{<br>  name: 'John',<br>  age: 15,<br>  city: 'NYC',<br>  marks: { <strong>english: 50<\/strong>, maths: 49 }<br>}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Using_Loops\"><\/span>4. Using Loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This is the regular way of looping through the original object and copying its each property to the new object. For this, we first create a new blank object. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user = {};<\/pre>\n\n\n\n<p>Then we loop through the original object, and one by one, copy its properties to the new object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for (let prop in user) {<br>   if (user.hasOwnProperty(prop)) {<br>       new_user[prop] = user[prop];<br>     }<br>   }<br><br>console.log(new_user); \/\/ {name: 'John', age: 15, city: 'NYC',  marks: { english: 50, maths: 49 }}<\/pre>\n\n\n\n<p>This will create a shallow copy. Let us change the marks.english property in new_user and see if it also changes the corresponding value in user object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_user.marks.english = 45<br>console.log(user); \/\/ {name: 'John', age: 15, city: 'NYC', marks: { <strong>english: 45<\/strong>, maths: 49 }}<br><br>console.log(new_user); \/\/ {name: 'John', age: 15, city: 'NYC',  marks: { <strong>english: 45<\/strong>, maths: 49 }}<\/pre>\n\n\n\n<p>As you can see, changing inner object property in cloned object also changes the original object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt several different ways to clone JavaScript objects. Among them, using spread operator and object.assign() function are the most convenient ways to clone JavaScript objects. But they both create shallow clones. If you want to do a deep clone, then you need to use JSON.parse() function. In this case, the original object needs to be JSON-ready. All these solutions clone JS object completely. Also, they are fast and scalable. However, if you only want to clone a specific part of your JS object, then you need to use the good old fashioned loop, where you individually clone each property. In this case, the performance will be slower than other methods due to the loop, but you can pick and choose which properties you want to clone. You can choose any of the above methods, depending on your requirements and tradeoffs therein.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-detect-browser-in-javascript\/\">How to Detect Browser in JavaScript<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-element-is-hidden-in-jquery\/\">How to Check if Element is Hidden in jQuery<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-correct-this-inside-callback\/\">How to Access Correct This Inside Callback<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often web developers need to clone JavaScript objects. Here are the different ways to clone object in JavaScript.<\/p>\n","protected":false},"author":1,"featured_media":4999,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[295],"tags":[317],"class_list":["post-4965","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-clone-object"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Clone Object in JavaScript - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Often web developers need to clone JavaScript objects. Here are the different ways to clone object in JavaScript.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Clone Object in JavaScript - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Often web developers need to clone JavaScript objects. Here are the different ways to clone object in JavaScript.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-09T07:21:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-09T07:21:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/clone-object-in-javascript.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Clone Object in JavaScript\",\"datePublished\":\"2024-10-09T07:21:36+00:00\",\"dateModified\":\"2024-10-09T07:21:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/\"},\"wordCount\":1337,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/clone-object-in-javascript.jpg?fit=300%2C200&ssl=1\",\"keywords\":[\"clone object\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/\",\"name\":\"How to Clone Object in JavaScript - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/clone-object-in-javascript.jpg?fit=300%2C200&ssl=1\",\"datePublished\":\"2024-10-09T07:21:36+00:00\",\"dateModified\":\"2024-10-09T07:21:39+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Often web developers need to clone JavaScript objects. Here are the different ways to clone object in JavaScript.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/clone-object-in-javascript.jpg?fit=300%2C200&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/clone-object-in-javascript.jpg?fit=300%2C200&ssl=1\",\"width\":300,\"height\":200,\"caption\":\"clone object in javascript\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-clone-object-in-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Clone Object in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Clone Object in JavaScript - Ubiq BI","description":"Often web developers need to clone JavaScript objects. Here are the different ways to clone object in JavaScript.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How to Clone Object in JavaScript - Ubiq BI","og_description":"Often web developers need to clone JavaScript objects. Here are the different ways to clone object in JavaScript.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-10-09T07:21:36+00:00","article_modified_time":"2024-10-09T07:21:39+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/clone-object-in-javascript.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Clone Object in JavaScript","datePublished":"2024-10-09T07:21:36+00:00","dateModified":"2024-10-09T07:21:39+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/"},"wordCount":1337,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/clone-object-in-javascript.jpg?fit=300%2C200&ssl=1","keywords":["clone object"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/","name":"How to Clone Object in JavaScript - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/clone-object-in-javascript.jpg?fit=300%2C200&ssl=1","datePublished":"2024-10-09T07:21:36+00:00","dateModified":"2024-10-09T07:21:39+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Often web developers need to clone JavaScript objects. Here are the different ways to clone object in JavaScript.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/clone-object-in-javascript.jpg?fit=300%2C200&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/clone-object-in-javascript.jpg?fit=300%2C200&ssl=1","width":300,"height":200,"caption":"clone object in javascript"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-clone-object-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Clone Object in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/clone-object-in-javascript.jpg?fit=300%2C200&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1i5","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/4965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=4965"}],"version-history":[{"count":49,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/4965\/revisions"}],"predecessor-version":[{"id":5015,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/4965\/revisions\/5015"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/4999"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=4965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=4965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=4965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}