Returns the index of the last occurrence of value in the array, or -1 if value is not present. Uses the native lastIndexOf function if possible

返回值在数组中最后出现的位置,如果没有出现,返回-1

1 _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
2 => 4

源码:

1  _.lastIndexOf = function(array, item) {
2     if (array == null) return -1;
3     if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
4     var i = array.length;
5     while (i--) if (i in array && array[i] === item) return i;
6     return -1;
7   };

 

 

 

posted on 2012-04-15 23:39  himanhimao  阅读(279)  评论(0编辑  收藏  举报