Forked from DenisIzmaylov/circular-json-stringify.js
Created
October 22, 2018 03:58
-
-
Save WooodHead/ef4aa94e2e94a4996fa6fcc5f13ae2a6 to your computer and use it in GitHub Desktop.
Circular JSON Stringify
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 circularJSONStringify(obj) { | |
const cache = []; | |
const result = JSON.stringify(obj, (key, value) => { | |
if (typeof value === 'object' && value !== null) { | |
if (cache.indexOf(value) !== -1) { | |
// Circular reference found, discard key | |
return; | |
} | |
// Store value in our collection | |
cache.push(value); | |
} | |
return value; | |
}); | |
cache.length = 0; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment