# prefer-array-flat-map

Rule `unicorn/prefer-array-flat-map`. Prefer `.flatMap(…)` over `.map(…).flat()` and `.filter(…).flatMap(…)`.

- Source: unicorn
- Technologies: JavaScript
- Kind: performance
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 40.5M weekly downloads of presets that enable it
- Trend: +81.2% last quarter
- Rank: #420 by reach, #412 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-array-flat-map` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-array-flat-map.md) |
| Oxlint | `unicorn/prefer-array-flat-map` | no | safe | plugin `unicorn`, category perf | [docs](https://oxc.rs/docs/guide/usage/linter/rules/unicorn/prefer-array-flat-map.html) |
| Biome | `complexity/useFlatMap` | yes | safe | group complexity, since 1.0.0 | [docs](https://biomejs.dev/linter/rules/use-flat-map/) |

## Code examples

### Incorrect: the rule reports this

```js
const foo = bar.map(element => unicorn(element)).flat();
```

```js
const foo = bar.map(element => unicorn(element)).flat(1);
```

```js
const foo = bar
  .filter(element => element.isUnicorn)
  .flatMap(element => [element.name, element.alias]);
```

### Correct: the rule accepts this

```js
const foo = bar.flatMap(element => unicorn(element));
```

```js
const foo = bar.flatMap(element => element.isUnicorn ? [element.name, element.alias] : []);
```

```js
const foo = bar.map(element => unicorn(element)).flat(2);
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-array-flat-map": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "unicorn/prefer-array-flat-map": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "complexity": {
        "useFlatMap": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Biome recommended](https://lintradar.dev/presets/biome-recommended.md) | Biome | `@biomejs/biome` | 491 | 28.5M | +106.8% |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [Ultracite for Biome](https://lintradar.dev/presets/ultracite-biome.md) | Biome | `ultracite` | 616 | 2.1M | +151.7% |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/no-useless-switch-case](https://lintradar.dev/rules/unicorn/no-useless-switch-case.md): Disallow useless case in switch statements.
- [unicorn/prefer-array-index-of](https://lintradar.dev/rules/unicorn/prefer-array-index-of.md): Prefer `Array#{indexOf,lastIndexOf}()` over `Array#{findIndex,findLastIndex}()` when looking for the index of an item.
- [unicorn/no-magic-array-flat-depth](https://lintradar.dev/rules/unicorn/no-magic-array-flat-depth.md): Disallow a magic number as the `depth` argument in `Array#flat(…).`
- [unicorn/prefer-bigint-literals](https://lintradar.dev/rules/unicorn/prefer-bigint-literals.md): Prefer `BigInt` literals over the constructor.
- [unicorn/no-document-cookie](https://lintradar.dev/rules/unicorn/no-document-cookie.md): Do not use `document.cookie` directly.
- [unicorn/prefer-date-now](https://lintradar.dev/rules/unicorn/prefer-date-now.md): Prefer `Date.now()` to get the number of milliseconds since the Unix Epoch.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-array-flat-map. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
