How to Clone a JavaScript Object
Cloning an object in JavaScript is a common task for any project: from simple objects to complicated ones.
As a rule, the assignment operator doesn’t generate a copy of an object. It is capable of assigning a reference to it.
Let’s check out the following code:
The object variable is a container for a newly initialized object. The copy variable points to the same object and is a reference to the object. The object { a: 2, b: 3, } shows that there exist two ways of gaining success. This method can remove any form of immutability, leading to bugs.
There is a naive way to copy objects: it’s looping through the original one copying every property one after another. The code will look like this:
But note that there can be inherent issues, such as:
- objectCopy obtains a new Object.prototype method that differs from mainObject object prototype method. But it’s not what you want if you wish a copy of the original object.
- Property descriptors can not be copied. A “writable” descriptor, including a value set to be false, will be true within the objectCopy object.
- The code that was shown above copies the enumerable properties of mainObject.
- In case a property inside the original object is an object, it can be shared between the copy and the original one.
Notice that by nature, JavaScript objects are changeable and are stored as a reference. So, when assigning the object to another variable, you assign the memory address of the object to the variable. In such circumstances, the old and new objects point to the same memory address. Every change in one of them will be reflected in the other. Hence, assigning one object to another will not copy your object.
Shallow copy with Spread.Syntax or Object.assign
The spread syntax is one of the shortest and simplest methods of copying and/or merging objects.
The example will look as follows:
The Object.assign() method is applied for copying all the enumerable properties from one or more source objects to the targeted object returning it.
Here is an example:
So, the spread syntax and object.assign are standard ways to copy an object. They are equivalent.
The problem with the methods above is that they implement a shallow copy. So, a new object is generated, which obtains the precise copy of the values of the original object. But notice that if any of the object fields is a reference to the other objects, simply the reference address is copied: only the memory address is copied.
The example is as follows:
These methods are handy, but you are not recommended to use them when you have nested objects in the object that you want to copy.
Using JSON.stringify and JSON.parse
A deep copy takes place when you copy an object with the objects to which it refers. You can do it by applying the mixture of JSON.stringify and JSON.parse for creating a deep copy.
First of all you, should JSON.stringify() the json object parsing it to get the object back. The JSON.parse() method is targeted at parsing and constructing the JavaScript object or value that is described by the string. The new fields of the object have their memory address and are independent of nested object fields.
Here is an example of the deep cloning:
Describing JavaScript Objects
The purpose of the JavaScript objects is storing keyed collections of various data and more complicated entities.
JavaScript objects are included in all the language aspects; hence it is necessary to learn them as soon as you start to study it.
You can easily create objects using figure brackets {…} and having a list of properties is needed. Property is known as a “key: value”, in which key or property name is a string and value can be whatever.