Skip to content

shallowClone

Subhajit Sahu edited this page May 3, 2023 · 1 revision

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

Clone this wiki locally