Created
March 16, 2021 21:00
-
-
Save disgusting-dev/4a00dbdd2c8435d15df25c1b12411083 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 isObject(value) { | |
return typeof value === 'object'; | |
} | |
function deepCopy(value, hash = new Map()) { | |
let newVal; | |
if (hash.has(value)) { | |
return hash.get(value); | |
} | |
if (Array.isArray(value)) { | |
hash.set(value, newVal); | |
newVal = value.map(item => { | |
return deepCopy(item, hash); | |
}); | |
} else if (value && isObject(value)) { | |
newVal = {}; | |
Object.keys(value).forEach(key => { | |
if (isObject(value[key])) { | |
hash.set(value, newVal); | |
newVal[key] = deepCopy(value[key], hash); | |
} else { | |
newVal[key] = value[key]; | |
} | |
}); | |
} else { | |
newVal = value; | |
} | |
return newVal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment