Skip to main content

Function: any()

any<ElementType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => boolean): boolean

Advances the iterable until it finds a matching element, returning true if it's found and false otherwise.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>An iterator to search for a value in.
callbackFn(element: ElementType, index: number) => booleanA function to execute for each element produced by the iterator. It should return a truthy value to indicate the element passes the test, and a falsy value otherwise.

Returns

boolean

true if the callback function returns a truthy value for at least one element. Otherwise, false.

Examples

import { some } from '@sapphire/iterator-utilities';

const iterable = [1, 2, 3, 4, 5];
console.log(some(iterable, (value) => value % 2 === 0));
// Output: true
const iterable = [1, 2, 3, 4, 5];
console.log(some(iterable, (value) => value % 6 === 0));
// Output: false

Remarks

This function consumes the iterator until the value is found or the iterator is exhausted.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/some.ts:33