Question: I need the *index* of the first value in the array, that matches a custom compare function.

The very nice [underscorej](http://underscorejs.org/#find) has a "find" function that returns the first value where a function returns true, but I would need this that returns the index instead. Is there a version of indexOf available somewhere, where I can pass a function used to comparing?

Thanks for any suggestions!

Answer: 
As others have noted, easy enough to roll your own, which you can keep short and simple for your particular use case:

```
// Find the index of the first element in array
// meeting specified condition.
//
var findIndex = function(arr, cond) {
  var i, x;
  for (i in arr) {
    x = arr[i];
    if (cond(x)) return parseInt(i);
  }
};

var moreThanTwo = function(x) { return x > 2 }
var i = findIndex([1, 2, 3, 4], moreThanTwo)

```

Or if you're a CoffeeScripter:

```
findIndex = (arr, cond) ->
  for i, x of arr
    return parseInt(i) if cond(x)

```
The javascript array method **filter** returns a subset of the array that return true from the function passed. 

```
var arr= [1, 2, 3, 4, 5, 6],
first= arr.filter(function(itm){
    return itm>3;
})[0];
alert(first);

if you must support IE before #9 you can 'shim' Array.prototype.filter-

Array.prototype.filter= Array.prototype.filter || function(fun, scope){
    var T= this, A= [], i= 0, itm, L= T.length;
    if(typeof fun== 'function'){
        while(i<L){
            if(i in T){
                itm= T[i];
                if(fun.call(scope, itm, i, T)) A[A.length]= itm;
            }
            ++i;
        }
    }
    return A;
}

```