Created
July 1, 2014 00:03
-
-
Save jmervine/56d47d08c476430d2e22 to your computer and use it in GitHub Desktop.
fairlyDeepClone ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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