Nodejs Array Find by Index findIndex(callback)

Here you can find the source of findIndex(callback)

Method Source Code

'use strict';//from  ww  w  .  j a v  a 2s.  co m

Array.prototype.findIndex = Array.prototype.findIndex || function(callback) {
  if (this === null) {
    throw new TypeError('Array.prototype.findIndex called on null or undefined');
  } else if (typeof callback !== 'function') {
    throw new TypeError('callback must be a function');
  }
  var list = Object(this);
  // Makes sures is always has an positive integer as length.
  var length = list.length >>> 0;
  var thisArg = arguments[1];
  for (var i = 0; i < length; i++) {
    if ( callback.call(thisArg, list[i], i, list) ) {
      return i;
    }
  }
  return -1;
};

Related

  1. findIndex(fn, v)
    Array.prototype.findIndex = function(fn, v) {
        var isFN = typeof(fn) === 'function';
        var isV = v !== undefined;
        for (var i = 0, len = this.length; i < len; i++) {
            if (isFN) {
                if (fn(this[i], i)) {
                    return i;
                continue;
    ...
    
  2. findIndex(obj)
    Array.prototype.findIndex = function (obj) {
      for (var i = 0, imax = this.length; i < imax; i++) {
        var ectypeObj = this[i];
        var ectypeObjLength = 0, successLength = 0;
        for (var k in ectypeObj) {
          ectypeObjLength += 1;
          if (ectypeObj[k] === obj[k]) {
            successLength += 1;
        if (ectypeObjLength === successLength) {
          return i;
      return -1;