Create a copy of an object in JavaScript
This post will discuss how to create a copy of an object in JavaScript.
1. Using Object.assign() function
The Object.assign() method is used to copy all enumerable own properties from the source object to a target object. You can use this method to create a copy of an object without references, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
const obj = { string: 'primitive string', number: 100, boolean: true, null: null, date: new Date(), undefined: undefined, infinity: Infinity, regexp: /(\w+)\s(\w+)/ } const clone = Object.assign({}, obj); console.log(clone); /* Output: { string: 'primitive string', number: 100, boolean: true, null: null, date: 2020-01-26T14:29:25.074Z, undefined: undefined, infinity: Infinity, regexp: /(\w+)\s(\w+)/ } */ |
2. Using Spread operator
With ES6, you can use the Spread operator (...) to create a shallow copy of the object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
const obj = { string: 'primitive string', number: 100, boolean: true, null: null, date: new Date(), undefined: undefined, infinity: Infinity, regexp: /(\w+)\s(\w+)/ } const clone = { ...obj }; console.log(clone); /* Output: { string: 'primitive string', number: 100, boolean: true, null: null, date: 2020-01-26T14:29:25.074Z, undefined: undefined, infinity: Infinity, regexp: /(\w+)\s(\w+)/ } */ |
3. Using jQuery
With jQuery, the $.extend() method can be used to create a shallow copy of an object. It works by copying all non-nullish properties from one object to another object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
const { JSDOM } = require("jsdom"); const { window } = new JSDOM(); var $ = require("jquery")(window); const obj = { string: 'primitive string', number: 100, boolean: true, null: null, date: new Date(), undefined: undefined, // lost infinity: Infinity, regexp: /(\w+)\s(\w+)/ } const clone = $.extend({}, obj); console.log(clone); /* Output: { string: 'primitive string', number: 100, boolean: true, null: null, date: 2020-01-26T14:37:13.666Z, infinity: Infinity, regexp: /(\w+)\s(\w+)/ } */ |
If the first argument to $.extend is true, it performs a deep copy.
4. Using Underscore/Lodash Library
If you’re already using the Underscore or Lodash library, you can use the _.clone method for creating a shallow copy of the specified object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
var _ = require('underscore'); const obj = { string: 'primitive string', number: 100, boolean: true, null: null, date: new Date(), undefined: undefined, infinity: Infinity, regexp: /(\w+)\s(\w+)/ } const clone = _.clone(obj); console.log(clone); /* Output: { string: 'primitive string', number: 100, boolean: true, null: null, date: 2020-01-26T14:38:50.835Z, undefined: undefined, infinity: Infinity, regexp: /(\w+)\s(\w+)/ } */ |
Alternatively, you can use the _.extend method, similar to jQuery’s $.extend() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
var _ = require('underscore'); const obj = { string: 'primitive string', number: 100, boolean: true, null: null, date: new Date(), undefined: undefined, infinity: Infinity, regexp: /(\w+)\s(\w+)/ } const clone = _.extend({}, obj); console.log(clone); /* Output: { string: 'primitive string', number: 100, boolean: true, null: null, date: 2020-01-26T14:41:36.353Z, undefined: undefined, infinity: Infinity, regexp: /(\w+)\s(\w+)/ } */ |
5. Custom Routine
In pure JavaScript, you can write a custom routine for copying an object. The following code can be used as a starting point for all your copying needs:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
function clone(obj) { if (obj == null || typeof obj !== 'object') { return obj; } if (obj instanceof Date) { return new Date(obj); } if (obj instanceof RegExp) { return new RegExp(obj); } // handle other types if required var clonedObj = (obj instanceof Array) ? [] : {}; for (var key in obj) { clonedObj[key] = clone(obj[key]); } return clonedObj; }; const obj = { string: 'primitive string', number: 100, boolean: true, null: null, date: new Date(), undefined: undefined, infinity: Infinity, regexp: /(\w+)\s(\w+)/ } console.log(clone(obj)); /* Output: { string: 'primitive string', number: 100, boolean: true, null: null, date: 2020-01-26T14:33:50.020Z, undefined: undefined, infinity: Infinity, regexp: /(\w+)\s(\w+)/ } */ |
That’s all about creating a copy of an object in JavaScript.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)