Created
July 3, 2015 14:12
-
-
Save jasdeepkhalsa/38d8de659d79b459ae22 to your computer and use it in GitHub Desktop.
Traverse a JS object to find a key or value
This file contains 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
// 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