Skip to content

findIndex

js
_.findIndex(array, [predicate=_.identity], [fromIndex=0])

此方法与 _.find 类似,不同之处在于它返回 predicate 返回真值的第一个元素的索引,而不是元素本身。

¥This method is like _.find except that it returns the index of the first element predicate returns truthy for instead of the element itself.

新增于

¥Since

1.1.0

参数

¥Arguments

  1. array (数组):要检查的数组。

    ¥array (Array): The array to inspect.

  2. [predicate=_.identity] (函数):每次迭代调用的函数。

    ¥[predicate=_.identity] (Function): The function invoked per iteration.

  3. [fromIndex=0] (数值):要从中搜索的索引。

    ¥[fromIndex=0] (number): The index to search from.

返回

¥Returns

(数值):返回找到的元素的索引,否则为 -1

¥(number): Returns the index of the found element, else -1.

示例

¥Example

js
var users = [
  { 'user': 'barney',  'active': false },
  { 'user': 'fred',    'active': false },
  { 'user': 'pebbles', 'active': true }
];

_.findIndex(users, function(o) { return o.user == 'barney'; });
// => 0

// The `_.matches` iteratee shorthand.
_.findIndex(users, { 'user': 'fred', 'active': false });
// => 1

// The `_.matchesProperty` iteratee shorthand.
_.findIndex(users, ['active', false]);
// => 0

// The `_.property` iteratee shorthand.
_.findIndex(users, 'active');
// => 2

Lodash v4.17 中文网 - 粤ICP备13048890号