Skip to content

Instantly share code, notes, and snippets.

@rikukissa
Last active December 20, 2015 03:09
Show Gist options
  • Save rikukissa/6061635 to your computer and use it in GitHub Desktop.
Save rikukissa/6061635 to your computer and use it in GitHub Desktop.
Object recursion, requires underscore.js
var walk = function(obj, iterator) {
for(i in obj) {
if(_.isObject(obj[i]) && !_.isArray(obj[i]) && !_.isFunction(obj[i])) {
walk(obj[i], iterator);
continue;
}
iterator(obj[i]);
}
};
var obj = {
a: 1,
b: 2,
c: {
d: {
e: 'e'
},
f: 2,
g: 'foobar',
h: null,
i: function() {}
}
};
walk(obj, function(item) {
console.log(item);
});
// 1
// 2
// e
// 2
// foobar
// null
// function () {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment