Skip to content

findLastIndex

js
_.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length-1])

此方法与 _.findIndex 类似,不同之处在于它从右到左迭代 collection 的元素。

¥This method is like _.findIndex except that it iterates over elements of collection from right to left.

新增于

¥Since

2.0.0

参数

¥Arguments

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

    ¥array (Array): The array to inspect.

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

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

  3. [fromIndex=array.length-1] (数值):要从中搜索的索引。

    ¥[fromIndex=array.length-1] (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': true },
  { 'user': 'fred',    'active': false },
  { 'user': 'pebbles', 'active': false }
];

_.findLastIndex(users, function(o) { return o.user == 'pebbles'; });
// => 2

// The `_.matches` iteratee shorthand.
_.findLastIndex(users, { 'user': 'barney', 'active': true });
// => 0

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

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

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