(Deep) copying an array using jQuery
Since Array.slice() does not do deep copying, it is not suitable for multidimensional arrays:
var a =[[1], [2], [3]];
var b = a.slice();
b.shift().shift();
// a is now [[], [2], [3]]
Note that although I've used shift().shift()
above, the point is just that b[0][0]
contains a pointer to a[0][0]
rather than a value.
Likewise delete(b[0][0])
also causes a[0][0]
to be deleted and b[0][0]=99
also changes the value of a[0][0]
to 99.
jQuery's extend
method does perform a deep copy when a true value is passed as the initial argument:
var a =[[1], [2], [3]];
var b = $.extend(true, [], a);
b.shift().shift();
// a is still [[1], [2], [3]]
Is there a method to clone an array in jQuery?
Just use Array.prototype.slice
.
a = [1];
b = a.slice();
JSFiddle - http://jsfiddle.net/neoswf/ebuk5/
How to clone a Javascript Array of Objects?
EDIT
Deep clone use JSON.parse(JSON.stringify(arr));
Shallow clone Use slice(0);
var arr = [{'obj1':1}, {'obj2':2}];var clone = arr.slice(0);console.log(clone);
How do you clone an array of objects in JavaScript?
Creating a deep copy with structuredClone
The modern way to deep copy an array in JavaScript is to use structuredClone:
array2 = structuredClone(array1);
However, this function is relatively new (Chrome 98, Firefox 94) and is currently only available to about 85% of users, so it's not ready for production yet without a polyfill.
As an alternative, you can use one of the well-supported JSON-based solutions below.
Creating a deep copy with JSON.parse
A general solution, that accounts for all possible objects inside an Array of objects may not be possible.
That said, if your array contains objects that have JSON-serializable content (no functions, no Number.POSITIVE_INFINITY
, etc.) one simple way to avoid loops, at a performance cost, is this pure vanilla one-line solution.
let clonedArray = JSON.parse(JSON.stringify(nodesArray))
To summarize the comments below, the primary advantage of this approach is that it also clones the contents of the array, not just the array itself. The primary downsides are its limit of only working on JSON-serializable content, and it's performance is ~30 times slower than the spread method.
If you have shallow objects in the array, and IE6 is acceptable, a better approach is to use the spread operator combined with the .map array operator. For a two levels deep situation (like the array in the Appendix below):
clonedArray = nodesArray.map(a => {return {...a}})
The reasons are two fold: 1) It is much, much faster (see below for a benchmark comparison) and it will also allow any valid object in your array.
*Appendix:
The performance quantification is based on cloning this array of objects a million times:
[{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic1.jpg?raw=true', id: '1', isFavorite: false}, {url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic2.jpg?raw=true', id: '2', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic3.jpg?raw=true', id: '3', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic4.jpg?raw=true', id: '4', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic5.jpg?raw=true', id: '5', isFavorite: true},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic6.jpg?raw=true', id: '6', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic7.jpg?raw=true', id: '7', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic8.jpg?raw=true', id: '8', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic9.jpg?raw=true', id: '9', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic10.jpg?raw=true', id: '10', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic11.jpg?raw=true', id: '11', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic12.jpg?raw=true', id: '12', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic13.jpg?raw=true', id: '13', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic14.jpg?raw=true', id: '14', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic15.jpg?raw=true', id: '15', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic16.jpg?raw=true', id: '16', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic17.jpg?raw=true', id: '17', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic18.jpg?raw=true', id: '18', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic19.jpg?raw=true', id: '19', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic20.jpg?raw=true', id: '20', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic21.jpg?raw=true', id: '21', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic22.jpg?raw=true', id: '22', isFavorite: false},{url: 'https://github.com/bobziroll/scrimba-react-bootcamp-images/blob/master/pic23.jpg?raw=true', id: '23', isFavorite: false}]
either using:
let clonedArray = JSON.parse(JSON.stringify(nodesArray))
or:
clonedArray = nodesArray.map(a => {return {...a}})
The map/spread approach took 0.000466 ms per pass and the JSON.parse and JSON.stringify 0.014771 ms per pass.*
jquery.extend(true, [], obj) not creating a deep copy
And now here is the real answer:
At the moment jQuery can only clone plain JavaScript Objects, while you're using custom ones. And that's obvious, since jQuery cannot know how exactly to instantiate a new custom object. So this works as expected:
var george = {};
george.favorite_books = ["Curious George"];
var kate = {};
kate.favorite_books = ["The Da Vinci Code", "Harry Potter"];
var people = [kate, george];
var people_copy = $.extend(true, [], people);
console.log(people_copy[0].favorite_books == people[0].favorite_books);
Reference to a jQuery code: https://github.com/jquery/jquery/blob/master/src/core.js#L305
See that it checks if it's jQuery.isPlainObject(copy)
or it's an array. Otherwise it performs just a reference copy.
What is the most efficient way to deep clone an object in JavaScript?
Native deep cloning
There's now a JS standard called "structured cloning", that works experimentally in Node 11 and later, will land in browsers, and which has polyfills for existing systems.
structuredClone(value)
If needed, loading the polyfill first:
import structuredClone from '@ungap/structured-clone';
See this answer for more details.
Older answers
Fast cloning with data loss - JSON.parse/stringify
If you do not use Date
s, functions, undefined
, Infinity
, RegExps, Maps, Sets, Blobs, FileLists, ImageDatas, sparse Arrays, Typed Arrays or other complex types within your object, a very simple one liner to deep clone an object is:
JSON.parse(JSON.stringify(object))
const a = {
string: 'string',
number: 123,
bool: false,
nul: null,
date: new Date(), // stringified
undef: undefined, // lost
inf: Infinity, // forced to 'null'
re: /.*/, // lost
}
console.log(a);
console.log(typeof a.date); // Date object
const clone = JSON.parse(JSON.stringify(a));
console.log(clone);
console.log(typeof clone.date); // result of .toISOString()
jquery $.extend recursive deep copy
Technically there is no different from r and r2.
But please take note on the $.extend() behaviour http://api.jquery.com/jQuery.extend/
NOTE 1 jQuery.extend( target [, object1 ] [, objectN ] )
var r = $.extend(object1 , object2 );
// mean now we merge object2 into object1 so object1={ a: 'a', b: 2, c: 'c'}
console.log(object1) // return { a: 'a', b: 2, c: 'c'}
console.log(r) // return { a: 'a', b: 2, c: 'c'}
NOTE 2 jQuery.extend( [deep ], target, object1 [, objectN ] )
var objA = { k: 1, b: 2};
var objB = { k: 2, b: 'b', z:9};
var ret = $.extend(true, objA, objB); // when true as first parameter = merge recursive (deep copy)
console.log(objA); // return { k: 2, b: 'b', z:9}
console.log(ret); // return { k: 2, b: 'b', z:9}
NOTE 3 What happen if we pass False or {} as first param.
var objA = { k: 1, b: 2};
var objB = { k: 2, b: 'b', z:9};
var ret = $.extend(false, objA, objB);
console.log(objA) // return { k: 1, b: 2}
console.log(ret) // return { k: 2, b: 'b', z:9}
From the above example we can see True deep copy will update target value accordingly by other objectN value
Related Topics
Injecting a Mock into an Angularjs Service
Es6 Iterate Over Class Methods
Dynamically Arrange Some Elements Around a Circle
How to Highlight the Text of the Dom Range Object
JavaScript Getelementbyid() Not Working
Accessing an Object's Property from an Event Listener Call in JavaScript
How to Perform Flood Fill with HTML Canvas
How to Implement Custom Sort to a Specific Column After Jqgrid Has Been Generated
JavaScript - Convert Array of Arrays into Array of Objects with Prefilled Values
Browser Event When Downloaded File Is Saved to Disk
New Line in JavaScript Alert Box
How to Check If a Number Is Between Two Values
Merge Two Arrays So That the Values Alternate
Automatic Semicolon Insertion & Return Statements
Indirect Function Call in JavaScript
When to Use the JavaScript Mime Type Application/JavaScript Instead of Text/Javascript
Jqgrid: Change Background Color of Row Based on Row Cell Value by Column Name