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)

```
How about such find function ?

```
(function () {
  if (!Array.prototype._find) {
    Array.prototype._find = function (value) {
      var i = -1, j = this.length;
      if (typeof(value)=="function") 
         for(; (++i < j) && !value(this[i]););
      else
         for(; (++i < j) && !(this[i] === value););

      return i!=j ? i : -1;
    }
  }
}());

```