Lodash _.findIndex

2021-09-24 09:35 更新

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

該方法類似_.find,區(qū)別是該方法返回第一個通過 predicate 判斷為真值的元素的索引值(index),而不是元素本身。

引入版本

1.1.0

參數(shù)

  1. array (Array): 要搜索的數(shù)組。
  2. [predicate=_.identity] (Array|Function|Object|string): 這個函數(shù)會在每一次迭代調(diào)用。
  3. [fromIndex=0] (number): The index to search from.

返回值

(number): 返回找到元素的 索引值(index),否則返回 -1。

例子

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


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號