Created
May 8, 2017 08:07
-
-
Save yuqianma/ea202ddba39f1438ee4feeba78b022b8 to your computer and use it in GitHub Desktop.
[noCircularJson.js]
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 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