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).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.
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).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.

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.