menu

manual-filter

Prefer Array.filter over manually collecting matching elements

Creating an array, conditionally pushing each input element, and returning it spells Array.filter manually. Instead, you SHOULD return the result of filter directly.

Reported

1function positive(values: int32[]): int32[] {2    const result: int32[] = [];3    for (const value of values) {4        if (value > 0) {5            result.push(value);6        }7    }8    return result;9}

Accepted

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