findIndex.js 520 B

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