Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Created April 25, 2014 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebnilsson/11282295 to your computer and use it in GitHub Desktop.
Save sebnilsson/11282295 to your computer and use it in GitHub Desktop.
Get the first matching item in array, based on predicate-function
function matchFirst(arr, predicateFn) {
var index = -1;
arr.some(function(x, i) {
var isMatch = predicateFn(x, i);
if (isMatch) {
index = i;
return true;
}
});
if (index >= 0) {
return arr[index];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment