JavaScript

Clone Object Without Reference in JavaScript

In JavaScript, cloning the object is quite simple and easy for primitive data types. However, various techniques, including “deep cloning” and “shallow cloning” are used for copying objects without reference. Deep cloning is used for creating new objects and then cloning the particular object’s key-value pairs. In deep cloning, the defined objects are completely cloned within another object. On the other hand, in shallow cloning, an object is copied according to the bit-wise level.

This tutorial will explain the method for cloning objects without reference in JavaScript.

How to Clone Object Without Reference in JavaScript?

To clone an object without referencing, there are various methods that can be invoked, such as “object.assign()” and “JSON.parse()”. However, if you use the assignment operator to clone the object it will store the value in a variable along with an object on the rightmost side.

For practical implications, try out the stated methods one by one.

Method 1: Clone Objects Using “object.assign()” Method

In this stated method, the “object.assign()” method will be invoked for the purpose of cloning objects without reference in JavaScript. This method is invoked for copying the values and properties from one source object to a particular targeted object. To use this method, follow the below-stated syntax:

Object.assign(target, source)

 

Make an object to store the value:

const object = {x:2,y:5,z:9};

 

Utilize the “Object.assign()” method and set the source and target object values as the parameters of this method:

const clone = Object.assign({},object);

 

Assign a new value to the “x”:

object.x = 7

 

Invoke the “console.log()” method and pass the “object” as an argument to show the result:

console.log(object);

 

Invoke the console.log() method and pass the “clone” object to output the result:

console.log(clone);

 

It can be observed that the objects are cloned without reference in JavaScript:

Method 2: Clone Objects Using JSON.parse() Method

To perform cloning of the objects without referencing, the JavaScript “JSON.parse()” and “JSON.stringify()” methods will be invoked which are deep cloning techniques. For a profound understanding, follow the below-stated syntax:

JSON.parse(JSON.stringify(object)

 

First the “stringify()” method is invoked as the parameter of the “parse()” method to convert the defined JavaScript object into a string.

In this stated example, make a constant-type nested object:

const object = {a:9,b:10,c:{d:11}};

 

Invoke the “parse()” method and pass the “stringify()” method as the argument that will be used for converting a specified object into a particular string. Then, the “parse()” method is invoked to perform the operation for parsing and returning an object:

const deepClone = JSON.parse(JSON.stringify(object));

 

Set the value of the nested objects for cloning:

object.c.d = 39;

 

Invoke the “console.log()” method and pass the object as an argument to show the result:

console.log(object);

 

Then, pass the “deepClone” object as an argument of the “console.log()” method for displaying the object which was cloned:

console.log(deepClone);

 

That’s all about the clone object without reference in JavaScript. Furthermore, if you want to explore more about the difference between shallow cloning and deep cloning follow this article Shallow Copy vs Deep Copy in JavaScript.

Conclusion

To clone an object without referencing in JavaScript, there are number of methods that can be invoked, such as “JSON.parse()” and “object.assign()”. The “parse()” method is invoked to perform the operation for parsing and returning an object. This tutorial has stated the method for cloning the object without reference in JavaScript.

About the author

Hafsa Javed