Last active
December 27, 2017 09:36
-
-
Save heypoom/bb402c794d48c9d16055227a1dd032ed to your computer and use it in GitHub Desktop.
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
function clone(data) { | |
if (Array.isArray(data)) { | |
return data.map(clone) | |
} else if (data.constructor === Object) { | |
return Object.entries(data) | |
.map(([key, value]) => ({[key]: clone(value)})) | |
.reduce((obj, cur) => ({...obj, ...cur})) | |
} | |
return data | |
} | |
function clone2(src) { | |
const dest = Array.isArray(src) ? [] : {} | |
for (let key in src) { | |
const value = src[key] | |
dest[key] = typeof value === "object" ? copy(value) : value | |
} | |
return dest | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment