menu

prefer-array-search

Prefer direct Array search operations

Indirect searches allocate arrays, discard indices, or invoke callbacks for builtin equality. Instead, you SHOULD use the direct Array search matching that result.

find and findLast return an element. some and includes return membership. indexOf and lastIndexOf return a position. Reverse searches can change predicate or comparison order and require review.

Reported

1function firstPositive(values: int32[]): int32 | undefined {2    return values.filter((value) => value > 0).at(0);3}

Accepted

1function firstPositive(values: int32[]): int32 | undefined {2    return values.find((value) => value > 0);3}