When copying an array in JavaScript to another array:
var arr1 = ['a','b','c'];
var arr2 = arr1;
arr2.push('d'); //Now, arr1 = ['a','b','c','d']
I realized that arr2
refers to the same array as arr1
, rather than a new, independent array. How can I copy the array to get two independent arrays?
When we want to copy an array using the assignment operator (
=
) it doesn't create a copy it merely copies the pointer/reference to the array. For example:Often when we transform data we want to keep our initial datastructure (e.g. Array) intact. We do this by making a exact copy of our array so this one can be transformed while the initial one stays intact.
Ways of copying an array:
Be careful when arrays or objects are nested!:
When arrays are nested the values are copied by reference. Here is an example of how this could lead to issues:
So don't use these methods when there are objects or arrays inside your array you want to copy. i.e. Use these methods on arrays of primitives only.
If you do want to deepclone a javascript array use
JSON.parse
in conjunction withJSON.stringify
, like this:Performance of copying:
So which one do we choose for optimal performance. It turns out that the most verbose method, the
for
loop has the highest performance. Use thefor
loop for really CPU intensive copying (large/many arrays).After that the
.slice()
method also has decent performance and is also less verbose and easier for the programmer to implement. I suggest to use.slice()
for your everyday copying of arrays which aren't very CPU intensive. Also avoid using theJSON.parse(JSON.stringify(arr))
(lots of overhead) if no deep clone is required and performance is an issue.Source performance test
You can do that in following way :
arr2 = arr1.map(x => Object.assign({}, x));
In my particular case I needed to ensure the array remained intact so this worked for me:
Using jQuery deep copy could be made as following:
If your array contains elements of the primitive data type such as int, char, or string etc then you can user one of those methods which returns a copy of the original array such as .slice() or .map() or spread operator(thanks to ES6).
or
or
BUT if your array contains complex elements such as objects(or arrays) or more nested objects, then, you will have to make sure that you are making a copy of all the elements from the top level to the last level else reference of the inner objects will be used and that means changing values in object_elements in new_array will still affect the old_array. You can call this method of copying at each level as making a DEEP COPY of the old_array.
For deep copying, you can use the above-mentioned methods for primitive data types at each level depending upon the type of data or you can use this costly method(mentioned below) for making a deep copy without doing much work.
There are a lot of other methods out there which you can use depending on your requirements. I have mentioned only some of those for giving a general idea of what happens when we try to copy an array into the other by value.
Here's a variant: