Using .filter() to get only the first or last matching element creates an unnecessary intermediate array.
The .find() method is more efficient as it stops iteration once a match is found.
Similarly, .findLast() is more efficient for finding the last matching element.
This rule reports:
.filter()[0] or .filter().shift() or .filter().at(0) → prefer .find()
.filter().pop() or .filter().at(-1) → prefer .findLast()
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.
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>.shift(): number |undefined
Removes the first element from an array and returns it.
If the array is empty, undefined is returned and the array is not modified.
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).at(0);
Error ts(2550) ― Property 'at' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later.
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>.pop(): number |undefined
Removes the last element from an array and returns it.
If the array is empty, undefined is returned and the array is not modified.
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).at(-1);
Error ts(2550) ― Property 'at' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later.
Returns the value of the first element in the array where predicate is true, and undefined
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, find
immediately returns that element value. Otherwise, find returns undefined.
@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.
find((
value: number
value)=>
value: number
value>0);
declare const
const array:number[]
array:number[];
const array:number[]
array.findLast((
value: any
value)=>
value: any
value>0);
Error ts(2550) ― Property 'findLast' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.
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.
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.
If you need the full filtered array for other purposes beyond accessing the first element, you can disable this rule.
However, in most cases where only the first match is needed, .find() is the better choice.