filter
Import from @varavel/vdl-plugin-sdk/utils/maps.
function filter<K, V>(map, callback): Map<K, V>;
Filters a Map based on a predicate function.
This function takes a Map and a predicate function, and returns a new Map containing only the entries for which the predicate function returns true.
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 filter. |
callback | (value, key, map) => boolean | A predicate function that tests each entry. |
Returns
Map<K, V>
A new Map containing only the entries that satisfy the predicate.
Example
const map = new Map([
['a', 1],
['b', 2],
['c', 3],
['d', 4]
]);
const result = filter(map, (value) => value > 2);
// result will be:
// Map(2) {
// 'c' => 3,
// 'd' => 4
// }