Skip to content

arrayFinds

Reports using .filter()[0] instead of .find() when looking for a single element.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

When searching for the first item in an array matching a condition, using .filter(...)[0] is less efficient than using .find(...). The .find() method stops searching after finding the first match, whereas .filter() iterates through the entire array.

const
const values: number[]
values
= [1, 2, 3, 4, 5];
const
const first: number
first
=
const values: number[]
values
.
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
> 3)[0];
const
const users: {
name: string;
}[]
users
= [{
name: string
name
: "Alice" }, {
name: string
name
: "Bob" }];
const
const user: {
name: string;
}
user
=
const users: {
name: string;
}[]
users
.
Array<{ name: string; }>.filter(predicate: (value: {
name: string;
}, index: number, array: {
name: string;
}[]) => unknown, thisArg?: any): {
name: string;
}[] (+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
((
user: {
name: string;
}
user
) =>
user: {
name: string;
}
user
.
name: string
name
=== "Alice")[0];

This rule is not configurable.

If you intentionally use .filter(...)[0] to execute side effects in the callback on all array elements before returning the first match, you may want to disable this rule. However, this pattern is unusual and may indicate a need for code refactoring.

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