Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davemackintosh/4250004 to your computer and use it in GitHub Desktop.
Save davemackintosh/4250004 to your computer and use it in GitHub Desktop.
JavaScript recursive array iterator
/**
* Prototype for recursively iterating over an array of
* arrays.
* @author Dave Mackintosh
* @param callback function
*/
Array.prototype.recursiveArrayIterator = function (cb) {
this.forEach(function (obj, inx) {
// If it's an array we want to fire another iterator
// with our parent array as a separate argument
if (obj.constructor === Array) {
obj.recursiveArrayIterator.call(obj, cb);
} else {
// If it isn't an array, fire the callback
// with helpful arguments
cb.apply(this, [obj, inx]);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment