Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
Add Comment
Please, Sign In to add comment