Last active
August 29, 2015 14:07
-
-
Save ivan-loh/a4a4c3e49a6134bb62de to your computer and use it in GitHub Desktop.
function to go thru every single values in a json object
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 traverse() { | |
| var prop, addr, value, | |
| doc = arguments['0'], | |
| parent = arguments['1'], | |
| func = arguments['2']; | |
| if (typeof parent === "function") { | |
| func = parent; | |
| parent = undefined; | |
| } | |
| parent = parent ? parent + '_' : ''; | |
| for (prop in doc) { | |
| if (!doc.hasOwnProperty(prop)) { continue; } | |
| addr = parent + prop; | |
| value = doc[prop]; | |
| if (typeof value === "object") { | |
| traverse(value, addr, func); | |
| } else { | |
| func(addr.split('_'), value); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment