Skip to content

Instantly share code, notes, and snippets.

@exquisiteOntologist
Created June 22, 2016 23:44
Show Gist options
  • Select an option

  • Save exquisiteOntologist/71be19ae59d0b16401406414e796aa5b to your computer and use it in GitHub Desktop.

Select an option

Save exquisiteOntologist/71be19ae59d0b16401406414e796aa5b to your computer and use it in GitHub Desktop.
Duplicates an object and returns the duplicated values
export function duplicate(obj) {
if(obj === null || typeof(obj) !== 'object' || 'isActiveClone' in obj) {
if (obj !== null && obj !== undefined) {
if (obj.constructor === Array) {
return obj.slice(0);
}
}
return obj;
}
let temp = obj.constructor(); // changed
for(let key in obj) {
if(Object.prototype.hasOwnProperty.call(obj, key)) {
obj['isActiveClone'] = null;
temp[key] = duplicate(obj[key]);
delete obj['isActiveClone'];
}
}
return temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment