VDL Plugin SDK

find

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

function find<T>(set, doesMatch): T | undefined;

Finds the first element in a Set for which the predicate function returns true.

This function iterates through the elements of the Set and returns the first element for which the predicate function returns true. If no element satisfies the predicate, it returns undefined.

Type Parameters

Type ParameterDescription
TThe type of elements in the Set.

Parameters

ParameterTypeDescription
setSet<T>The Set to search.
doesMatch(value, value2, set) => booleanA predicate function that tests each element.

Returns

T | undefined

The first element that satisfies the predicate, or undefined if none found.

Example

const set = new Set([
  { name: 'apple', quantity: 10 },
  { name: 'banana', quantity: 5 },
  { name: 'grape', quantity: 15 }
]);
const result = find(set, (value) => value.quantity > 10);
// result will be: { name: 'grape', quantity: 15 }