Skip to content

arrayFilteredFinds

Reports using .filter() when only the first or last matching element is needed.

✅ This rule is included in the ts logical and logicalStrict presets.

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()
declare const
const array: number[]
array
: number[];
const array: number[]
array
.
Array<number>.filter(predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): number[] (+1 overload)

Returns the elements of an array that meet the condition specified in a callback function.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg 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)[0];
declare const
const array: number[]
array
: number[];
const array: number[]
array
.
Array<number>.filter(predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): number[] (+1 overload)

Returns the elements of an array that meet the condition specified in a callback function.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg 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.

shift
();
declare const
const array: number[]
array
: number[];
const array: number[]
array
.
Array<number>.filter(predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): number[] (+1 overload)

Returns the elements of an array that meet the condition specified in a callback function.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg 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.

@paramindex The zero-based index of the desired code unit. A negative index will count back from the last item.

at
(0);
declare const
const array: number[]
array
: number[];
const array: number[]
array
.
Array<number>.filter(predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): number[] (+1 overload)

Returns the elements of an array that meet the condition specified in a callback function.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg 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.

pop
();
declare const
const array: number[]
array
: number[];
const array: number[]
array
.
Array<number>.filter(predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): number[] (+1 overload)

Returns the elements of an array that meet the condition specified in a callback function.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg 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.

@paramindex The zero-based index of the desired code unit. A negative index will count back from the last item.

at
(-1);

This rule is not configurable.

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.

Made with ❤️‍🔥 around the world by the Flint team and contributors.