Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptHow to clone a JavaScript object ?

How to clone a JavaScript object ?

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 a few ways.

  • By iterating through each property and copying them to the new object.
  • Using JSON method as the source object MUST be JSON-safe. So it needs exception handling to keep it safe in the case which source object can not be convertible to JSON.
  • object.assign: This method does only a shallow copy. It means that nested properties are still copied by reference.               
  • Using Spread operator                                                                                                                                                                    

Let’s see them one by one with the help of Examples. 

Example 1: The one is to iterate through the 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. 

html




<h1 style="color:green;">
    neveropen
</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>


Output:

clone a JavaScript object

Example 2:This example uses JSON. Using this method, source object MUST be JSON safe.

HTML




<h1 style="color:green;">
    neveropen
</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>


Output:

clone a JavaScript object

Example 3:This method uses the Object.assign method.
 

Javascript




<h1 style="color:green;">
    neveropen
</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>


Output:

clone a JavaScript object

Example 4:This method uses the spread operator. 

HTML




<h1 style="color:green;">
    neveropen
</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 ={...sourceObject};
    document.getElementById("demo").innerHTML =
        "targetObject a = "+tO.a+", b = " + tO.b+", c = "+tO.c;
    }
</script>


Output:

clone a JavaScript object

clone a JavaScript object

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments