主题
indexOf
js
_.indexOf(array, value, [fromIndex=0])
使用 SameValueZero
进行相等比较,获取在 array
中找到第一个 value
的索引。如果 fromIndex
为负,则将其用作距 array
末尾的偏移量。
¥Gets the index at which the first occurrence of value
is found in array
using SameValueZero
for equality comparisons. If fromIndex
is negative, it's used as the offset from the end of array
.
新增于
¥Since
0.1.0
参数
¥Arguments
array
(数组):要检查的数组。¥
array
(Array): The array to inspect.value
(*):要搜索的值。¥
value
()*: The value to search for.[fromIndex=0]
(数值):要从中搜索的索引。¥
[fromIndex=0]
(number): The index to search from.
返回
¥Returns
(数值):返回匹配值的索引,否则为 -1
。
¥(number): Returns the index of the matched value, else -1
.
示例
¥Example
js
_.indexOf([1, 2, 1, 2], 2);
// => 1
// Search from the `fromIndex`.
_.indexOf([1, 2, 1, 2], 2, 2);
// => 3