Skip to content

deepClone

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

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)


References

Clone this wiki locally