Skip to content

Function: findKey()

ts
function findKey<T>(object, predicate): keyof T | undefined;

Finds the first key whose value matches a predicate. 查找第一个满足断言条件的键。

Type Parameters

Type ParameterDescription
T extends Record<string, unknown>The source object type / 源对象类型

Parameters

ParameterTypeDescription
objectTThe source object / 源对象
predicate(value, key) => booleanThe match predicate / 匹配断言

Returns

keyof T | undefined

The first matching key, or undefined / 第一个匹配的键,或 undefined

Example

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

Since

1.2.0

Released under the MIT License.