Skip to content

findKey

Import from @varavel/vdl-plugin-sdk/utils/objects.

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

Finds the key of the first element in the object that satisfies the provided testing function.

Type Parameters

Type Parameter
T extends Record\<any, any>

Parameters

Parameter Type Description
obj T The object to search.
predicate (value, key, obj) => boolean The function to execute on each value in the object. It takes three arguments: - value: The current value being processed in the object. - key: The key of the current value being processed in the object. - obj: The object that findKey was called upon.

Returns

keyof T | undefined

The key of the first element in the object that passes the test, or undefined if no element passes.

Example

const users = {
  'barney':  { 'age': 36, 'active': true },
  'fred':    { 'age': 40, 'active': false },
  'pebbles': { 'age': 1,  'active': true }
};
findKey(users, function(o) { return o.age < 40; }); => 'barney'