_.findIndex(array, [predicate=_.identity], [fromIndex=0])
该方法类似 _.find,但它返回的是谓词返回真值的第一个元素的索引,而不是元素本身。
自
1.1.0
参数
array (Array): 要检查的数组。
[predicate=_.identity] (Function): 每次迭代调用的函数。
[fromIndex=0](数字): 要开始搜索的索引。
返回值
(数字): 返回找到的元素的索引,否则返回-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
相关内容
- 介绍 Lodash:简化 JavaScript 编程,提供高效的集合操作和函数式编程支持。
- \_.chunk(array, [size=1]) _.chunk(array, [size=1]) 创建一个元素数组,将其分为长度为 size 的组。 如果数组不能均匀分割,则最终的块将是剩余的元素。
- \_.compact(array) _.compact(array) 创建一个删除所有错误值的数组。 值 false、null、0、""、undefined 和 NaN 均为 false。
- \_.concat(array, [values]) _.concat(array, [values]) 创建一个新数组,将数组与任何其他数组和/或值连接起来。
- \_.difference(array, [values]) _.difference(array, [values]) 使用 SameValueZero 进行等价比较,创建一个数组,其中的数组值不包含在其他给定数组中。结...
- \_.differenceBy(array, [values], [iteratee=_.identity]) .differenceBy(array, [values], [iteratee=.identity]) 该方法与 _.difference 类似,但它接受 i...