shallowClone - nodef/extra-array GitHub Wiki

Shallow clone an array.

Alternatives: shallowClone, deepClone.

function shallowClone(x)
// x: an array
const xarray = require('extra-array');

var x = [1, 2, 3, [4, 5]];
var y = xarray.shallowClone(x);
// → [ 1, 2, 3, [ 4, 5 ] ]  (y shallow cloned)

y[1] = 0; y;
// → [ 1, 0, 3, [ 4, 5 ] ]  (y modified)

x;
// → [ 1, 2, 3, [ 4, 5 ] ]  (x not modified)

y[3][0] = 0; y;
// → [ 1, 0, 3, [ 0, 5 ] ]  (y modified)

x;
// → [ 1, 2, 3, [ 0, 5 ] ]  (x modified)

References