How to clone a JavaScript object? - GeeksforGeeks

27 Mar, 2019
Cloning a JavaScript object is a task that is used mostly because we do not want to create the same object if the same object already exists.There are few ways.
By iterate through each property and copy them to new object.
Using JSON method as the source object MUST be JSON safe. So it need exception handling to keep it safe in case in which source object can not be convertible to JSON.
object.assignThis method does only a shallow copy. It means that nested properties are still copied by reference.
Let’s see them one by one with the help of Examples.Example 1: The one is to iterate through source object’s properties and copy all of them one by one to the target object. It is quite simple but not used very often.
<!DOCTYPE html>  <html>      <head>         <title></title>    </head>             <body style = "text-align:center;">          <h1 style = "color:green;" >              GeeksForGeeks          </h1>          <p id="demo2">sourceObject = {a:1, b:2, c:3};</p>            <button onClick="fun()">click        </button>       <p id="demo"></p>         <script>        function fun(){          const sourceObject = {a:1, b:2, c:3};          let tO = {};            for (let prop in sourceObject) {              if (sourceObject.hasOwnProperty(prop)) {                tO[prop] = sourceObject[prop];              }            }           document.getElementById("demo").innerHTML =              "targetObject a = "+tO.a+", b = " + tO.b+", c = "+tO.c;          }        </script>     </body>  </html>
Output:
Before we clicked the button:
After clicking the button:Example 2:This example uses JSON. Using this method, source object MUST be JSON safe.<!DOCTYPE html>  <html>      <head>         <title></title>    </head>     <body style = "text-align:center;">          <h1 style = "color:green;" >              GeeksForGeeks          </h1>          <p id="demo2">sourceObject = {a:1, b:2, c:3};</p>            <button onClick="fun()">click        </button>       <p id="demo"></p>        <script>        function fun(){          const sourceObject = {a:1, b:2, c:3};          let tO = {};          tO = JSON.parse(JSON.stringify(sourceObject));          document.getElementById("demo").innerHTML =            "targetObject a = "+tO.a+", b = " + tO.b+", c = "+tO.c;          }        </script>     </body>  </html>Output:Before we clicked the button:After clicking the button:Example 3:This method uses the Object.assign method.<!DOCTYPE html>  <html>      <head>         <title></title>    </head>     <body style = "text-align:center;">              <h1 style = "color:green;" >              GeeksForGeeks          </h1>          <p id="demo2">sourceObject = {a:1, b:2, c:3};</p>            <button onClick="fun()">click        </button>       <p id="demo"></p>           <script>        function fun(){          const sourceObject = {a:1, b:2, c:3};          let tO = {};          tO = Object.assign({}, sourceObject);          document.getElementById("demo").innerHTML =             "targetObject a = "+tO.a+", b = " + tO.b+", c = "+tO.c;          }        </script>     </body>  </html>Output:Before we clicked the button:After clicking the button:My Personal Notes
arrow_drop_upSave
<!DOCTYPE html>  <html>      <head>         <title></title>    </head>     <body style = "text-align:center;">          <h1 style = "color:green;" >              GeeksForGeeks          </h1>          <p id="demo2">sourceObject = {a:1, b:2, c:3};</p>            <button onClick="fun()">click        </button>       <p id="demo"></p>        <script>        function fun(){          const sourceObject = {a:1, b:2, c:3};          let tO = {};          tO = JSON.parse(JSON.stringify(sourceObject));          document.getElementById("demo").innerHTML =            "targetObject a = "+tO.a+", b = " + tO.b+", c = "+tO.c;          }        </script>     </body>  </html>
Output:
Before we clicked the button:
After clicking the button:Example 3:This method uses the Object.assign method.<!DOCTYPE html>  <html>      <head>         <title></title>    </head>     <body style = "text-align:center;">              <h1 style = "color:green;" >              GeeksForGeeks          </h1>          <p id="demo2">sourceObject = {a:1, b:2, c:3};</p>            <button onClick="fun()">click        </button>       <p id="demo"></p>           <script>        function fun(){          const sourceObject = {a:1, b:2, c:3};          let tO = {};          tO = Object.assign({}, sourceObject);          document.getElementById("demo").innerHTML =             "targetObject a = "+tO.a+", b = " + tO.b+", c = "+tO.c;          }        </script>     </body>  </html>Output:Before we clicked the button:After clicking the button:My Personal Notes
arrow_drop_upSave
<!DOCTYPE html>  <html>      <head>         <title></title>    </head>     <body style = "text-align:center;">              <h1 style = "color:green;" >              GeeksForGeeks          </h1>          <p id="demo2">sourceObject = {a:1, b:2, c:3};</p>            <button onClick="fun()">click        </button>       <p id="demo"></p>           <script>        function fun(){          const sourceObject = {a:1, b:2, c:3};          let tO = {};          tO = Object.assign({}, sourceObject);          document.getElementById("demo").innerHTML =             "targetObject a = "+tO.a+", b = " + tO.b+", c = "+tO.c;          }        </script>     </body>  </html>
Output:
Before we clicked the button:
After clicking the button:My Personal Notes
arrow_drop_upSave
JavaScript-Misc
javascript-object
Picked
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to get character array from string in JavaScript?
How to filter object array based on attributes?
How to remove duplicate elements from JavaScript Array ?
Lodash _.debounce() Method
Angular File Upload
How to get selected value in dropdown list using JavaScript ?