Created
November 22, 2021 19:20
-
-
Save tyhenry/7ffd69d61ddc0a350895d9d9e102b6e1 to your computer and use it in GitHub Desktop.
javascript deep clone
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
const clone = (objIn) => { | |
if (objIn === null || typeof (objIn) !== 'object') { | |
return objIn; // must be object to clone, otherwise we return value | |
} | |
const objOut = Array.isArray(objIn) ? [] : {}; | |
Object.keys(objIn).forEach((key) => { | |
objOut[key] = clone(objIn[key]); | |
}); | |
return objOut; | |
}; | |
export default clone; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment