Returns the index at which value can be found in the array, or -1 if value is not present in the array. Uses the native indexOf function unless it's missing. If you're working with a large array, and you know that the array is already sorted, pass truefor isSorted to use a faster binary search.

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

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

源码:

 1   _.indexOf = function(array, item, isSorted) {
 2     if (array == null) return -1;
 3     var i, l;
 4     if (isSorted) {
 5       i = _.sortedIndex(array, item);
 6       return array[i] === item ? i : -1;
 7     }
 8     if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
 9     for (i = 0, l = array.length; i < l; i++) if (i in array && array[i] === item) return i;
10     return -1;
11   };

 

 

 

 

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