Skip to content

Instantly share code, notes, and snippets.

@heypoom
Last active December 27, 2017 09:36
Show Gist options
  • Save heypoom/bb402c794d48c9d16055227a1dd032ed to your computer and use it in GitHub Desktop.
Save heypoom/bb402c794d48c9d16055227a1dd032ed to your computer and use it in GitHub Desktop.
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