deepClone (original) (raw)
Deep clone an array.
Alternatives: shallowClone, deepClone.
function deepClone(x) // x: an array
const xarray = require('extra-array');
var x = [1, 2, 3, [4, 5]]; var y = xarray.deepClone(x); // → [ 1, 2, 3, [ 4, 5 ] ] (y deep cloned)
y[3][0] = 0; y; // → [ 1, 2, 3, [ 0, 5 ] ] (y modified)
x; // → [ 1, 2, 3, [ 4, 5 ] ] (x not modified)