Skip to content

Function: find()

ts
function find<T>(collection, predicate): T extends readonly U[] ? U : T[keyof T] | undefined;

Finds the first matching value in an array or object. 在数组或对象中查找第一个匹配的值。

Type Parameters

Type ParameterDescription
T extends Record<string, unknown> | readonly unknown[]The source collection type / 源集合类型

Parameters

ParameterTypeDescription
collectionTThe source collection / 源集合
predicate(value, key, collection) => booleanThe match predicate / 匹配断言

Returns

T extends readonly U[] ? U : T[keyof T] | undefined

The first matching value, or undefined / 第一个匹配的值,或 undefined

Example

ts
find({ a: 1, b: 2 }, (value) => value > 1)
// => 2

Since

1.2.0

Released under the MIT License.