Last active
August 29, 2015 14:22
-
-
Save NullEntity/abaaa27dace6be55eab0 to your computer and use it in GitHub Desktop.
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 removeCircularReferences(obj) { | |
debugger; | |
var cache = []; | |
var filter = function(obj, path) { | |
return _.reduceRight(obj, function(memo, v, k) { | |
if (typeof v === 'object' && v !== null) { | |
var index = cache.indexOf(v); | |
if (index !== -1) { | |
memo[k] = '__circular__@' + index; | |
return memo; | |
} else { | |
cache.push(v); | |
memo[k] = filter(v); | |
return memo; | |
} | |
} else { | |
memo[k] = v; | |
return memo; | |
} | |
}); | |
}; | |
return filter(obj); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment