Skip to content

No filter().map() Chain

Suggestion
small-rules/no-filter-map-chain

Disallow map(...) directly after filter(...).

Diagnostic Messages

avoidFilterMapChain
Do not chain map(...) directly after filter(...). Combine both operations in a single loop.

Configuration

This rule does not accept options.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/no-filter-map-chain": "error"
}
}

Examples

filter followed by map
const baseArray = [1, 2, 3, 4, 5, 6, 7];
const array = baseArray
.filter((value) => value % 2 === 0)
.map((value) => value * 2);
single-pass filtering and mapping
const baseArray = [1, 2, 3, 4, 5, 6, 7];
const filteredArray = new Array<number>();
for (const value of baseArray) {
if (value % 2 !== 0) continue;
filteredArray.push(value * 2);
}