findLastIndex.js 517 B

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