Skip to content

Instantly share code, notes, and snippets.

@zwilias
Created May 30, 2013 12:45
Show Gist options
  • Save zwilias/5677581 to your computer and use it in GitHub Desktop.
Save zwilias/5677581 to your computer and use it in GitHub Desktop.
Figure out the structure of an object to arbitrary depth in mongo (or anything else running V8)
function structure(anObject, maxDepth) {
if (maxDepth < 1) {
return Array.isArray(anObject)
? []
: typeof(anObject) === "object"
? {}
: typeof(anObject);
}
var clone;
if (typeof(anObject) === "object") {
clone = Array.isArray(anObject) ? [] : {};
for (var key in anObject) {
clone[key] = structure(anObject[key], maxDepth-1);
}
} else {
clone = typeof(anObject);
}
return clone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment