1

I want to deep copy an array of integers in NodeJS. The first method I tried is

obj2 = JSON.parse(JSON.stringify(obj1));

However, it is so slow because it is a big array.

Now I am using

var l = obj1.length;
while (l--){
  obj2.push(obj1[l]);
}

Though it is much faster, I am wondering if there exist better solutions? Thank you all!

isvforall
  • 8,768
  • 6
  • 35
  • 50
Luyao Wang
  • 269
  • 1
  • 3
  • 11

4 Answers4

2

If it's an array, did you try just

var arr2 = arr1.slice(0);

It does create a shallow copy, but for an array containing just primitive integers it shouldn't matter.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • 1
    Arguments of slice are [optional](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice), so `arr1.slice()` would do just fine. – Роман Парадеев Apr 25 '16 at 21:38
  • @РоманПарадеев - and still I decided to add `0` as an argument, to make it clear that we're slicing from the beginning. – adeneo Apr 25 '16 at 21:39
  • Why not explicitly set both indices then? – Роман Парадеев Apr 25 '16 at 21:42
  • @РоманПарадеев - because according to the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) the first argument isn't optional, only the second one is. – adeneo Apr 25 '16 at 21:45
  • @adeneo you show docs for **string**, for an array the first argument is [optional](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) – isvforall Apr 25 '16 at 21:46
  • @isvforall - yeah, thought I was clever there and noone would notice, but both arguments are optional. Most of the time though you'll see `slice(0)` for arrays, probably because it's so common to add the zero whenever you use `slice()` as it's needed for strings, and I always add it without even thinking about it. It doesn't really matter, if you leave it out, the engine will just insert a zero for you, same thing as writing yourself. – adeneo Apr 25 '16 at 22:15
  • Question clearly states "deep copy" – Michal M. Jan 24 '23 at 11:39
1

you can also make it like

var arr = [1,2,3,4,5,6,7,8],
    brr = arr.map(e => e);
Redu
  • 25,060
  • 6
  • 56
  • 76
0

If your array contains numbers only (it won't to work if your array contains arrays), you could use Array.prototype.slice function without arguments

var arr1 = [1, 2, 3, 4, 5];
var arr2 = arr1.slice();
isvforall
  • 8,768
  • 6
  • 35
  • 50
0

If you need to get a sorted array and save the information on the position of the element in the original array:

0) The original array:

var obj1 = [1, 2, 3, 100, 5, 0, 200, 30];

1) It is necessary to keep the original position:

var index = Object.keys( obj1 ).sort( function(a, b) {
  return obj1[a] - obj1[b];
});

// >> ["5", "0", "1", "2", "4", "7", "3", "6"]

2) Get a sorted array:

var obj2 = obj1.sort( function(a, b) {
  return a-b;
});

// >> [0, 1, 2, 3, 5, 30, 100, 200]

3) Find the original position of the element with new index '0':

var originalIndex = index[0];

// >> "5"
stdob--
  • 28,222
  • 5
  • 58
  • 73