Skip to content

Instantly share code, notes, and snippets.

@jmervine
Created July 1, 2014 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmervine/56d47d08c476430d2e22 to your computer and use it in GitHub Desktop.
Save jmervine/56d47d08c476430d2e22 to your computer and use it in GitHub Desktop.
fairlyDeepClone ...
//b = JSON.parse(JSON.stringify(a));
function fairlyDeepClone(o) {
if (Array.isArray(o)) {
// handle array
return o.map(function(i) {
return fairlyDeepClone(i);
});
} else if (typeof o === 'object') {
// handle object
var oo = {};
Object.keys(o).forEach(function(k) {
oo[k] = fairlyDeepClone(o[k]);
});
return oo;
} else {
return o;
}
}
a = [ [ 1, 2 ], { '3': [ 4, 5 ] }, 9, 'ten' ];
b = fairlyDeepClone(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment