some()
some<
T>(collection,predicate):boolean
some<
T>(collection,predicate):boolean
Checks if predicate returns truthy for any element of collection.
Use array.some() or Object.values().some() directly instead.
Type Parametersโ
T: Tโ
The type of elements in the array, or the object type.
Parametersโ
Overload 1:
collection: T[]โ
The collection to iterate over.
predicate: (value, index, array) => booleanโ
The function invoked per iteration.
Overload 2:
collection: Tโ
The collection to iterate over.
predicate: (value, key, object) => booleanโ
The function invoked per iteration.
Returns: booleanโ
true if any element passes the predicate check, else false.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
any (Ramda) ยท some (Lodash, es-toolkit, Effect) ยท โ (Remeda, Radashi, Modern Dash, Antfu)
Exampleโ
const numbers = [1, 2, 3, 4, 5];
// โ Deprecated approach
const hasEven = some(numbers, n => n % 2 === 0);
console.log(hasEven); // true
// โ
Recommended approach (for arrays)
const hasEvenNative = numbers.some(n => n % 2 === 0);
console.log(hasEvenNative); // true
// โ
Recommended approach (for objects)
const obj = { a: 1, b: 2, c: 3 };
const hasEvenObj = Object.values(obj).some(n => n % 2 === 0);
console.log(hasEvenObj); // true
How it works?โ
Checks if any element passes the predicate.
Deprecated: Use array.some() directly.
Native Equivalentโ
// โ some(arr, fn)
// โ
arr.some(fn)
Use Casesโ
Check if any item matches ๐โ
Check if at least one element satisfies a condition.
const users = [{ admin: false }, { admin: true }, { admin: false }];
users.some(u => u.admin);
// => true
Validate at least one selectionโ
Ensure at least one option is selected.
const options = [{ selected: false }, { selected: false }];
options.some(o => o.selected);
// => false (no selection)
Check for errorsโ
Determine if any validation errors exist.
const fields = [{ error: null }, { error: "Required" }, { error: null }];
fields.some(f => f.error !== null);
// => true
Check if user has a required roleโ
Verify that a user holds at least one of the required roles for access control. Fundamental pattern for role-based authorization in any application.
const userRoles = ["editor", "viewer"];
const requiredRoles = ["admin", "editor"];
const hasAccess = some(requiredRoles, (role) => userRoles.includes(role));
// => true (user has "editor")