Using .some() is the clearest and most efficient way to check if an array contains an element matching a condition.
Patterns like .filter(...).length > 0 or .findIndex(...) !== -1 are less readable and potentially less efficient.
Returns the elements of an array that meet the condition specified in a callback function.
@param ― predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
filter((
value: number
value) =>
value: number
value > 0).
Array<number>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length > 0;
const
const items:string[]
items = ["a", "b", "c"];
const
const hasMatch:boolean
hasMatch =
const items:string[]
items.
Array<string>.filter<"b">(predicate: (value:string, index:number, array:string[])=> value is "b", thisArg?: any): "b"[] (+1 overload)
Returns the elements of an array that meet the condition specified in a callback function.
@param ― predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
filter((
item: string
item) =>
item: string
item === "b").
Array<"b">.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length !== 0;
const
const values:number[]
values = [1, 2, 3];
const
const hasMatch:boolean
hasMatch =
const values:number[]
values.
Array<number>.findIndex(predicate: (value:number, index:number, obj:number[])=> unknown, thisArg?: any): number
Returns the index of the first element in the array where predicate is true, and -1
otherwise.
@param ― predicate find calls predicate once for each element of the array, in ascending
order, until it finds one where predicate returns true. If such an element is found,
findIndex immediately returns that element index. Otherwise, findIndex returns -1.
@param ― thisArg If provided, it will be used as the this value for each invocation of
predicate. If it is not provided, undefined is used instead.
findIndex((
value: number
value) =>
value: number
value > 2) !== -1;
const
const items:string[]
items = ["a", "b", "c"];
const
const exists:boolean
exists =
const items:string[]
items.findLastIndex((
item: any
item) =>
item: any
item === "a") !== -1;
Error ts(2550) ― Property 'findLastIndex' does not exist on type 'string[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.
Determines whether the specified callback function returns true for any element of an array.
@param ― predicate A function that accepts up to three arguments. The some method calls
the predicate function for each element in the array until the predicate returns a value
which is coercible to the Boolean value true, or until the end of the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function.
If thisArg is omitted, undefined is used as the this value.
Determines whether the specified callback function returns true for any element of an array.
@param ― predicate A function that accepts up to three arguments. The some method calls
the predicate function for each element in the array until the predicate returns a value
which is coercible to the Boolean value true, or until the end of the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function.
If thisArg is omitted, undefined is used as the this value.
Returns the elements of an array that meet the condition specified in a callback function.
@param ― predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
If you need the filtered array for other purposes beyond checking existence, or if you’re working in a codebase that prefers explicit length checks for consistency, you may want to disable this rule.