VDL Plugin SDK

findValue

Import from @varavel/vdl-plugin-sdk/utils/maps.

function findValue<K, V>(map, doesMatch): V | undefined;

Finds the first value in a Map for which the predicate function returns true.

This function iterates through the entries of the Map and returns the value of the first entry for which the predicate function returns true. If no entry satisfies the predicate, it returns undefined.

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 search.
doesMatch(value, key, map) => booleanA predicate function that tests each entry.

Returns

V | undefined

The value of the first entry that satisfies the predicate, or undefined if none found.

Example

const map = new Map([
  ['apple', { color: 'red', quantity: 10 }],
  ['banana', { color: 'yellow', quantity: 5 }],
  ['grape', { color: 'purple', quantity: 15 }]
]);
const result = findValue(map, (value) => value.quantity > 10);
// result will be: { color: 'purple', quantity: 15 }