How to create clone of any object using jQuery ?
Last Updated :
21 Jul, 2021
In this article, we will learn to create a clone of an object using jQuery. This can be achieved using the extend() method of jQuery. The extend() method is used to merge the contents of multiple objects into the object passed as the first parameter. This can be used to clone an array as we can pass an empty object as the first parameter.
An additional parameter deep can be used to make deep copies of the object. This will make the method copy the object recursively and is helpful when a complex and nested object has to be cloned. This parameter will have to be passed as the first parameter when a deep copy is needed. To understand the clone concept in jquery, please refer to jQuery clone() with Examples article.
Syntax:
// Create a clone of the object using the extend() method
let newObj = jQuery.extend({}, obj);
// Create a deep clone of the object using the deep parameter
let newDeepObj = jQuery.extend(true, {}, obj);
The examples below illustrate the above approach:
Example 1: In this example, we will create a clone of the object using this method.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://code.jquery.com/jquery-3.6.0.min.js">
</script>
</head>
<body>
<h1 style="color: green">GeeksforGeeks</h1>
<b>How to create clone of any object using jQuery?</b>
<p>The object to be cloned is:</p>
<pre>
{ name: "Sam", age: 5, date: Date.now() }
</pre>
<p>Please check the console for the cloned object</p>
<script>
let obj = { name: "Sam", age: 5, date: Date.now() };
// Clone the object using
// the extend() method
let newObj = jQuery.extend({}, obj);
// Print both the objects
console.log("Original Object", obj);
console.log("Clone Object", newObj);
</script>
</body>
</html>
Output:
Example 2: In this method, we will create a deep clone of the object by using the deep parameter.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://code.jquery.com/jquery-3.6.0.min.js">
</script>
</head>
<body>
<h1 style="color: green">GeeksforGeeks</h1>
<b>How to create clone of any object using jQuery?</b>
<p>The object to be cloned is:</p>
<pre>
{
name: "Logs",
content: ["Error", "Warning", "Information" ],
stats: {
isDebug: true,
lastError: "Fatal Error 03",
lastInfo: "Consoled"
}
};
</pre>
<p>Please check the console for the cloned object</p>
<script>
let obj = {
name: "Logs",
content: ["Error", "Warning", "Information"],
stats: {
isDebug: true,
lastError: "Fatal Error 03",
lastInfo: "Consoled",
},
};
// Create a deep clone of the object
// by using the deep parameter
let newObj = jQuery.extend(true, {}, obj);
// Print both the objects
console.log("Original Object", obj);
console.log("Clone Object", newObj);
</script>
</body>
</html>
Output:
Similar Reads
How to get the object's name using jQuery ? In this article, we will learn how to find the name of an element using jQuery. The name attribute can be applied to multiple elements in HTML and is used to specify a name for any element. The name attribute of any element can be found out using the attr() method. This method is used to find the va
2 min read
How to clone a block using jQuery ? In this article, we will learn how to clone a block using jQuery. The clone method in jQuery performs a deep copy of a set of matching elements. You can create a copy of the data as well as a copy of the event handlers. Whenever we are building a dynamic website then this method is very efficient an
2 min read
How to change the element id using jQuery ? The jQuery methods are used to change the element ID which are described below: jQuery attr() Method: This method set/return attributes and values of the selected elements. If this method is used to return the attribute value, it returns the value of first selected element. If this method is used to
3 min read
How to append a jQuery object to all paragraphs using jQuery ? In this article, we will see how to append a jQuery object to all paragraphs using jQuery. Append means we add something to an existing element. The object is used to define a container for an external resource like a page, a picture, a media player, or a plug-in application. Used Methods: ready() M
2 min read
How to create an HTML element using jQuery ? In this article, we will see how to create an HTML element using jQuery. To create and append the HTML element, we use the jQuery append() method. The jQuery append() method is used to insert some content at the end of the selected elements. Syntax: $(selector).append( content, function(index, html)
2 min read