Last active
March 12, 2018 12:45
-
-
Save Herteby/b75c1063cc1720752f16fa6b16dab264 to your computer and use it in GitHub Desktop.
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
function fixCircularRefs(object) { | |
let objectValues = [] | |
let cleanObjectJSON = JSON.stringify(object, removeCircularReferences) | |
return JSON.parse(cleanObjectJSON) | |
function removeCircularReferences (key, value) { | |
if (typeof value === 'object') { | |
if (objectValues.indexOf(value) !== -1) { | |
return 'Circular reference to object with key: ' + key | |
} else { | |
objectValues.push(value) | |
} | |
} | |
return value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment