Guest User

Untitled

a guest
Mar 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. function fixCircularRefs(object) {
  2. let objectValues = []
  3. let cleanObjectJSON = JSON.stringify(object, removeCircularReferences)
  4. return JSON.parse(cleanObjectJSON)
  5.  
  6. function removeCircularReferences (key, value) {
  7. if (typeof value === 'object') {
  8. if (objectValues.indexOf(value) !== -1) {
  9. return 'Circular reference to object with key: ' + key
  10. } else {
  11. objectValues.push(value)
  12. }
  13. }
  14. return value
  15. }
  16. }
Add Comment
Please, Sign In to add comment