Skip to content

Instantly share code, notes, and snippets.

@ivan-loh
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save ivan-loh/a4a4c3e49a6134bb62de to your computer and use it in GitHub Desktop.

Select an option

Save ivan-loh/a4a4c3e49a6134bb62de to your computer and use it in GitHub Desktop.
function to go thru every single values in a json object
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