VDL Plugin SDK

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 ParameterDescription
KThe type of keys in the Map.
VThe type of values in the Map.

Parameters

ParameterTypeDescription
mapMap<K, V>The Map to filter.
callback(value, key, map) => booleanA 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
// }