Created
May 30, 2013 12:45
-
-
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)
This file contains hidden or 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 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