Created
December 22, 2011 17:58
-
-
Save CasualSuperman/1511207 to your computer and use it in GitHub Desktop.
Underscore.js indexBy mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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