Skip to content

Instantly share code, notes, and snippets.

@ephrin
Forked from sphvn/traverse.js
Created January 12, 2023 09:13
Show Gist options
  • Save ephrin/4bfe64ef070e05acdf08429853638f75 to your computer and use it in GitHub Desktop.
Save ephrin/4bfe64ef070e05acdf08429853638f75 to your computer and use it in GitHub Desktop.
Recursively traverse object javascript, recurse json js, loop and get key/value pair for JSON
var traverse = function(o, fn) {
for (var i in o) {
fn.apply(this,[i,o[i]]);
if (o[i] !== null && typeof(o[i])=="object") {
traverse(o[i], fn);
}
}
}
// usage
var obj = {'your':'object'};
traverse(obj, function(k,v){
console.log(k + " : " + v);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment