Skip to content
← Back to rules

unicorn/prefer-array-flat-map Perf

🛠️ 💡 An auto-fix and a suggestion are available for this rule.

What it does

Prefers a single .flatMap() over .map().flat() or .filter().flatMap().

Why is this bad?

A single .flatMap(…) avoids creating an intermediate array.

Examples

Examples of incorrect code for this rule:

javascript
const bar = [1, 2, 3].map((i) => [i]).flat();
const result = values.filter((value) => value > 0).flatMap((value) => [value, value]);

Examples of correct code for this rule:

javascript
const bar = [1, 2, 3].flatMap((i) => [i]);
const result = values.flatMap((value) => (value > 0 ? [value, value] : []));

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/prefer-array-flat-map": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-array-flat-map": "error",
  },
});
bash
oxlint --deny unicorn/prefer-array-flat-map

Version

This rule was added in v0.0.14.

References