Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Created July 3, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepkhalsa/38d8de659d79b459ae22 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/38d8de659d79b459ae22 to your computer and use it in GitHub Desktop.
Traverse a JS object to find a key or value
// MODIFIED FROM: http://stackoverflow.com/a/722732/1365289
function process(key, value) {
if (key === 'myKey') {
console.error(key, value);
}
}
function traverse(o, func) {
for (var i in o) {
/*jshint validthis: true */
func.apply(this, [i, o[i]]);
if (o[i] !== null && typeof(o[i]) == "object") {
//going on step down in the object tree!!
traverse(o[i], func);
}
}
}
traverse(myObj, process);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment