Javascript Array findIndex(predicate)

Description

Javascript Array findIndex(predicate)


/**/*from  w w  w  . j  a  v a  2s. c  o  m*/
 * @fileoverview
 *
 * Array.prototype.findIndex() polyfill, by
 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex MDN}.
 */

Array.prototype.findIndex = function(predicate) {
 if (this === null) {
  throw new TypeError('Array.prototype.findIndex called on null or undefined');
 }
 if (typeof predicate !== 'function') {
  throw new TypeError('predicate must be a function');
 }
 var list = Object(this);
 var length = list.length >>> 0;
 var thisArg = arguments[1];
 var value;

 for (var i = 0; i < length; i++) {
  value = list[i];
  if (predicate.call(thisArg, value, i, list)) {
   return i;
  }
 }
 return -1;
};



PreviousNext

Related