lastIndexOf.js 678 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Array lastIndexOf
  3. */
  4. function lastIndexOf(arr, item, fromIndex) {
  5. if (arr == null) {
  6. return -1;
  7. }
  8. var len = arr.length;
  9. fromIndex = (fromIndex == null || fromIndex >= len)? len - 1 : fromIndex;
  10. fromIndex = (fromIndex < 0)? len + fromIndex : fromIndex;
  11. while (fromIndex >= 0) {
  12. // we iterate over sparse items since there is no way to make it
  13. // work properly on IE 7-8. see #64
  14. if (arr[fromIndex] === item) {
  15. return fromIndex;
  16. }
  17. fromIndex--;
  18. }
  19. return -1;
  20. }
  21. module.exports = lastIndexOf;