Skip to content

findKey

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

function findKey<K, V>(map, doesMatch): K | undefined;

Finds the first key in a Map for which the predicate function returns true.

This function iterates through the entries of the Map and returns the key of the first entry for which the predicate function returns true. If no entry satisfies the predicate, it returns undefined.

Type Parameters

Type Parameter Description
K The type of keys in the Map.
V The type of values in the Map.

Parameters

Parameter Type Description
map Map\<K, V> The Map to search.
doesMatch (value, key, map) => boolean A predicate function that tests each entry.

Returns

K | undefined

The key of the first entry that satisfies the predicate, or undefined if none found.

Example

const map = new Map([
  ['apple', { color: 'red', quantity: 10 }],
  ['banana', { color: 'yellow', quantity: 5 }],
  ['grape', { color: 'purple', quantity: 15 }]
]);
const result = findKey(map, (value) => value.quantity > 10);
// result will be: 'grape'