Skip to content

Instantly share code, notes, and snippets.

@Herteby
Last active March 12, 2018 12:45
Show Gist options
  • Save Herteby/b75c1063cc1720752f16fa6b16dab264 to your computer and use it in GitHub Desktop.
Save Herteby/b75c1063cc1720752f16fa6b16dab264 to your computer and use it in GitHub Desktop.
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