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?
You can also use ES6 spread operator to copy Array
Make copy of multidimensional array/object:
Thanks to James Padolsey for this function.
Source: Here
Here is how you can do it for array of arrays of primitives of variable depth:
https://jsbin.com/xemazog/edit?js,console
Adding to the solution of array.slice(); be aware that if you have multidimensional array sub-arrays will be copied by references. What you can do is to loop and slice() each sub-array individually
same things goes to array of objects, they will be copied by reference, you have to copy them manually
As we know in Javascript arrays and objects are by reference, but what ways we can do copy the array without changing the original array later one?
Here are few ways to do it:
Imagine we have this array in your code:
1) Looping through the array in a function and return a new array, like this:
2) Using slice method, slice is for slicing part of the array, it will slice some part of your array without touching the original, in the slice, if don't specify the start and end of the array, it will slice the whole array and basically make a full copy of the array, so we can easily say:
3) Also contact method, this is for merging two array, but we can just specify one of arrays and then this basically make a copy of the values in the new contacted array:
4) Also stringify and parse method, it's not recommended, but can be an easy way to copy Array and Objects:
5) Array.from method, this is not widely supported, before use check the support in different browsers:
6) ECMA6 way, also not fully supported, but babelJs can help you if you want to transpile:
In Javascript, deep-copy techniques depend on the elements in an array.
Let's start there.
Three types of elements
Elements can be: literal values, literal structures, or prototypes.
From these elements we can create three types of arrays.
Deep copy techniques depend on the three array types
Based on the types of elements in the array, we can use various techniques to deep copy.
Array of literal-values (type1)
The
[...myArray]
,myArray.splice(0)
,myArray.slice()
, andmyArray.concat()
techniques can be used to deep copy arrays with literal values (boolean, number, and string) only; where the Spread operator[...myArray]
has the best performance (https://measurethat.net/Benchmarks/Show/4281/0/spread-array-performance-vs-slice-splice-concat).Array of literal-values (type1) and literal-structures (type2)
The
JSON.parse(JSON.stringify(myArray))
technique can be used to deep copy literal values (boolean, number, string) and literal structures (array, object), but not prototype objects.All arrays (type1, type2, type3)
The jQuery
$.extend(myArray)
technique can be used to deep-copy all array-types. Libraries like Underscore and Lo-dash offer similar deep-copy functions to jQuery$.extend()
, yet have lower performance. More surprisingly,$.extend()
has higher performance than theJSON.parse(JSON.stringify(myArray))
technique http://jsperf.com/js-deep-copy/15.And for those developers that shy away from third-party libraries (like jQuery), you can use the following custom function; which has higher performance than $.extend, and deep-copies all arrays.
So to answer the question...
Question
Answer
Because
arr1
is an array of literal values (boolean, number, or string), you can use any deep copy technique discussed above, where the spread operator...
has the highest performance.