Skip to content

Instantly share code, notes, and snippets.

@NullEntity
Last active August 29, 2015 14:22
Show Gist options
  • Save NullEntity/abaaa27dace6be55eab0 to your computer and use it in GitHub Desktop.
Save NullEntity/abaaa27dace6be55eab0 to your computer and use it in GitHub Desktop.
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