Skip to content

findMap

Returns the first non-undefined result of mapping fn over array, short-circuiting as soon as one is found. Similar to array.map(fn).find(v => v !== undefined), but doesn’t map (or call fn on) the remaining items once a match is found.

null and undefined are treated as empty arrays and return undefined.

Available since v3.1.1

import { findMap } from '@helpers4/array';
// or, from the all-in-one package (same code, one install):
import { findMap } from 'helpers4/array';
findMap<T, R>(array: readonly T[] | null | undefined, fn: function): R | undefined
ParameterTypeDescription
arrayreadonly T[] | null | undefinedThe array to search
fnfunctionMaps each item (and its index) to a result, or `undefined` to keep looking

R | undefined — The first non-undefined result of fn, or undefined if none was found

Returns the mapped value, not the original item — stops as soon as one is found.

findMap([1, 2, 3, 4], n => (n % 2 === 0 ? n * 10 : undefined))
// => 20

Try several fallible lookups in priority order

Section titled “Try several fallible lookups in priority order”

Chain several ?? -style lookups per item and stop at the first one that succeeds anywhere in the array.

const byId = new Map([[2, 'b']]);
findMap([1, 2, 3], id => byId.get(id))
// => 'b'

View source on GitHub