Skip to content

Instantly share code, notes, and snippets.

@zkat
Last active December 15, 2015 17:30
Show Gist options
  • Select an option

  • Save zkat/5296480 to your computer and use it in GitHub Desktop.

Select an option

Save zkat/5296480 to your computer and use it in GitHub Desktop.
find/find_if search utilities
Array.prototype.find_if = function(test, opts) {
opts = opts || {};
var key = opts.key || identity;
var start = opts.start || 0;
var end = opts.end || this.length;
for (var i = 0; i < this.length; i++) {
if (test(key(this[i]))) {
return this[i];
}
}
return null;
};
Array.prototype.find = function(match, opts) {
opts = opts || {};
var test = opts.test || function(x,y) { return x === y; };
return this.find_if(function(x) { return test(match, x); }, opts);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment