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).
Array<number>.at(index: number): number |undefined
Returns the item located at the specified index.
@param ― index The zero-based index of the desired code unit. A negative index will count back from the last item.
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).
Array<number>.at(index: number): number |undefined
Returns the item located at the specified index.
@param ― index The zero-based index of the desired code unit. A negative index will count back from the last item.
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.
Returns the value of the last element in the array where predicate is true, and undefined
otherwise.
@param ― predicate findLast calls predicate once for each element of the array, in descending
order, until it finds one where predicate returns true. If such an element is found, findLast
immediately returns that element value. Otherwise, findLast 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.
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.