Skip to content

Instantly share code, notes, and snippets.

@CasualSuperman
Created December 22, 2011 17:58
Show Gist options
  • Save CasualSuperman/1511207 to your computer and use it in GitHub Desktop.
Save CasualSuperman/1511207 to your computer and use it in GitHub Desktop.
Underscore.js indexBy mixin
/**
* Underscore function that combines _.find and _.indexOf
* Source: https://github.com/documentcloud/underscore/issues/413
*
* _.indexBy([2, 6, 4, 7, 9, 0], function(num) {
* return num % 2 === 1;
* });
* => 3
*/
_.mixin({
indexBy: function(list, func) {
for (var i = 0, l = list.length; i < l; i++) {
if (func(list[i])) return i;
}
return -1;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment