Skip to content

Instantly share code, notes, and snippets.

@tyhenry
Created November 22, 2021 19:20
Show Gist options
  • Save tyhenry/7ffd69d61ddc0a350895d9d9e102b6e1 to your computer and use it in GitHub Desktop.
Save tyhenry/7ffd69d61ddc0a350895d9d9e102b6e1 to your computer and use it in GitHub Desktop.
javascript deep clone
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