_.findIndex(查找元素索引值)
语法:
_.findIndex(array, [predicate=_.identity], [fromIndex=0])
源代码链接:source
npm包链接:npm package
描述:
这个方法l类似于_.find
,但是只是返回差找到第一次的索引值,而不是返回值本身。
开始版本:1.1.0
参数:
array (Array)
: 需要检索的数组。[predicate=_.identity] (Function)
: 每个迭代函数调用。[fromIndex=0] (number)
: 开始搜索的索引值。
返回值:
(number)
: 返回查找到的元素索引值,否则f返回-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
注意:不同的方式代表:_.matches
,_.matchesProperty
,_.property
的简写法
Last updated
Was this helpful?