JAVASCRIPT   23

findIndex.js

Guest on 28th May 2021 03:34:36 AM

  1. var makeIterator = require('../function/makeIterator_');
  2.  
  3.     /**
  4.      * Returns the index of the first item that matches criteria
  5.      */
  6.     function findIndex(arr, iterator, thisObj){
  7.         iterator = makeIterator(iterator, thisObj);
  8.         if (arr == null) {
  9.             return -1;
  10.         }
  11.  
  12.         var i = -1, len = arr.length;
  13.         while (++i < len) {
  14.             if (iterator(arr[i], i, arr)) {
  15.                 return i;
  16.             }
  17.         }
  18.  
  19.         return -1;
  20.     }
  21.  
  22.     module.exports = findIndex;

Raw Paste


Login or Register to edit or fork this paste. It's free.