find()
find<
T>(array,predicate):T|undefined
Iterates over elements of collection, returning the first element predicate returns truthy for.
DEPRECATED
Use array.find(predicate) directly instead.
Reason:
Native equivalent method now available
Type Parametersโ
T: Tโ
The type of elements in the array.
Parametersโ
array: T[]โ
The array to search.
predicate: (value, index, array) => booleanโ
The function invoked per iteration.
Returns: T | undefinedโ
The matched element, or undefined if not found.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
find (Lodash, es-toolkit, Remeda, Ramda) ยท findFirst (Effect) ยท โ (Radashi, Modern Dash, Antfu)
Exampleโ
const users = [
{ name: 'John', age: 25 },
{ name: 'Jane', age: 30 },
{ name: 'Bob', age: 35 }
];
// โ Deprecated approach
const found = find(users, user => user.age > 28);
console.log(found); // { name: 'Jane', age: 30 }
// โ
Recommended approach
const foundNative = users.find(user => user.age > 28);
console.log(foundNative); // { name: 'Jane', age: 30 }
How it works?โ
Returns the first element that passes the predicate.
Deprecated: Use array.find() directly (ES2015).
Native Equivalentโ
// โ find(arr, predicate)
// โ
arr.find(predicate)
Use Casesโ
Locate item by condition ๐โ
Find a specific object in an array based on a matching condition.
const users = [{ id: 1, role: "admin" }, { id: 2, role: "user" }];
users.find(user => user.role === "admin");
// => { id: 1, role: "admin" }
Get matching configurationโ
Find a configuration entry that matches specific criteria.
const configs = [{ env: "dev", url: "localhost" }, { env: "prod", url: "api.com" }];
configs.find(c => c.env === process.env.NODE_ENV);
Retrieve first valid optionโ
Find the first item that meets validation criteria.
const methods = [{ type: "card", valid: false }, { type: "paypal", valid: true }];
methods.find(m => m.valid);
// => { type: "paypal", valid: true }