Skip to content

Instantly share code, notes, and snippets.

@yuqianma
Created May 8, 2017 08:07
Show Gist options
  • Save yuqianma/ea202ddba39f1438ee4feeba78b022b8 to your computer and use it in GitHub Desktop.
Save yuqianma/ea202ddba39f1438ee4feeba78b022b8 to your computer and use it in GitHub Desktop.
[noCircularJson.js]
function jsonStringify (obj) {
var cache = []
function filter (key, value) {
if (typeof value === 'object' && value != null) {
if (cache.includes(value)) {
return // may circular
}
cache.push(value)
}
return value
}
return JSON.stringify(obj, filter)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment