Skip to content

Instantly share code, notes, and snippets.

@piscis
Last active August 29, 2015 14:16
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 piscis/eaec26b3ea6b0a6fa94a to your computer and use it in GitHub Desktop.
Save piscis/eaec26b3ea6b0a6fa94a to your computer and use it in GitHub Desktop.
Traverse JSON Object
var obj = {
a:1,
b: {
c:2
},
d: [1,2,3,4]
};
function process(key,value) {
console.log(key,value)
}
function traverse(o,func) {
for (var i in o) {
func.apply(this,[i,o[i]]);
if (o[i] !== null && typeof(o[i])=="object") {
traverse(o[i],func);
}
}
}
traverse(obj,process);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment