Is there an indexOf in javascript to search an array with custom compare function

后端 未结 8 1745
执笔经年
执笔经年 2020-12-05 18:05

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

The very nice underscorej has a \"find\" function that returns the

8条回答
  •  无人及你
    2020-12-05 18:21

    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;
        }
      }
    }());
    

提交回复
热议问题