Skip to content

Instantly share code, notes, and snippets.

@nblackburn
Last active July 31, 2022 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nblackburn/3a927ea86f28efcc417bdfd4e2c24817 to your computer and use it in GitHub Desktop.
Save nblackburn/3a927ea86f28efcc417bdfd4e2c24817 to your computer and use it in GitHub Desktop.
Clone Object

Recursively clone an object.

Usage

const obj = {
    foo: 'bar'
}

cloneObj(obj);
const cloneObj = (obj) => {
const props = Object.getOwnPropertyNames(obj);
for (const name of props) {
const value = obj[name];
if (value && typeof value === 'object') {
cloneObj(value);
}
}
return Object.assign({}, obj);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment