主题
find
js
_.find(collection, [predicate=_.identity], [fromIndex=0])
迭代 collection
的元素,返回 predicate
返回真值的第一个元素。使用三个参数调用谓词:(value, index|key, collection)。
¥Iterates over elements of collection
, returning the first element predicate
returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).
新增于
¥Since
0.1.0
参数
¥Arguments
collection
(数组|对象):要检查的集合。¥
collection
(Array|Object): The collection to inspect.[predicate=_.identity]
(函数):每次迭代调用的函数。¥
[predicate=_.identity]
(Function): The function invoked per iteration.[fromIndex=0]
(数值):要从中搜索的索引。¥
[fromIndex=0]
(number): The index to search from.
返回
¥Returns
(*):返回匹配的元素,否则为 undefined
。
¥()*: Returns the matched element, else undefined
.
示例
¥Example
js
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'pebbles', 'age': 1, 'active': true }
];
_.find(users, function(o) { return o.age < 40; });
// => object for 'barney'
// The `_.matches` iteratee shorthand.
_.find(users, { 'age': 1, 'active': true });
// => object for 'pebbles'
// The `_.matchesProperty` iteratee shorthand.
_.find(users, ['active', false]);
// => object for 'fred'
// The `_.property` iteratee shorthand.
_.find(users, 'active');
// => object for 'barney'