Created
June 22, 2016 23:44
-
-
Save exquisiteOntologist/71be19ae59d0b16401406414e796aa5b to your computer and use it in GitHub Desktop.
Duplicates an object and returns the duplicated values
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
| 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