Skip to content

findKey

Returns the first key of a Map whose entry satisfies the predicate, in insertion order.

Available since v3.0.3

import { findKey } from '@helpers4/map';
// or, from the all-in-one package (same code, one install):
import { findKey } from 'helpers4/map';
findKey<K, V>(map: ReadonlyMap<K, V>, predicate: function): K | undefined
ParameterTypeDescription
mapReadonlyMap<K, V>The Map to search
predicatefunctionFunction invoked with (value, key)

K | undefined — The matching key, or undefined if none matches

Searches in insertion order and returns the key of the first match.

findKey(new Map([['a', 1], ['b', 2]]), value => value > 1)
// => 'b'

Returns undefined when nothing satisfies the predicate.

findKey(new Map([['a', 1]]), value => value > 10)
// => undefined

View source on GitHub