docs/reference/set/find.md
Sets)Finds the first element in a Set for which the predicate function returns true.
const element = find(set, doesMatch);
::: info
This function is available exclusively from es-toolkit/set to avoid potential conflicts with similar functions for other collection types.
:::
find(set, doesMatch)Use find when you want to find the first element in a Set that matches a specific condition. Provide a predicate function that tests each element, and it returns the first matching element or undefined if none is found.
import { find } from 'es-toolkit/set';
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: { name: 'grape', quantity: 15 }
You can search based on various criteria.
import { find } from 'es-toolkit/set';
// Find by value property
const users = new Set([
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 3, name: 'Charlie', age: 35 },
]);
const senior = find(users, user => user.age >= 35);
// Result: { id: 3, name: 'Charlie', age: 35 }
// Find by string pattern
const emails = new Set(['[email protected]', '[email protected]', '[email protected]']);
const adminEmail = find(emails, email => email.startsWith('admin'));
// Result: '[email protected]'
set (Set<T>): The Set to search.doesMatch ((value: T, value2: T, set: Set<T>) => boolean): A predicate function that tests each element.(T | undefined): The first element that satisfies the predicate, or undefined if none found.