Skip to content

@studnicky/v8/chained-array-iteration

Disallows chaining .map() and .filter() calls onto one another — .map().filter() or .filter().map(). Each call in the chain allocates a new intermediate array and iterates the collection separately. A single .reduce() call transforms and filters in one pass with one allocation.

Fixable: No · Options: No · Suggested severity: error

✗ Incorrect

ts
const names = users.filter((u) => u.active).map((u) => u.name);
ts
const doubled = values.map((v) => v * 2).filter((v) => v > 0);

✓ Correct

ts
const names = users.reduce<string[]>((acc, u) => {
  if (u.active) { acc.push(u.name); }
  return acc;
}, []);
ts
const doubled = values.reduce<number[]>((acc, v) => {
  const next = v * 2;
  if (next > 0) { acc.push(next); }
  return acc;
}, []);