Skip to content

filter

Creates a new Map containing only the entries for which the predicate returns true.

Available since v3.0.3

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

Map<K, V> — A new Map with only the matching entries

Creates a new Map with only the entries whose value satisfies the predicate.

filter(new Map([['a', 1], ['b', 2], ['c', 3]]), value => value % 2 === 0)
// => Map(1) { 'b' => 2 }

Returns an empty Map when nothing matches.

filter(new Map([['a', 1]]), () => false)
// => Map(0) {}

View source on GitHub