For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/unicorn/prefer-array-flat-map.md.
close

prefer-array-flat-map

Added in v0.6.5

Configuration

PresetConfigured Value
✅ unicornPlugin.configs.recommended"error"
rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/prefer-array-flat-map': 'error',
    },
  },
]);

Rule Details

Prefer Array#flatMap() over chaining Array#map() and Array#flat() when the flat depth is omitted or exactly 1.

Examples of incorrect code for this rule:

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

Examples of correct code for this rule:

const foo = bar.flatMap(element => unicorn(element));
const foo = bar.map(element => unicorn(element)).flat(2);
const foo = React.Children.map(children, fn).flat();

Original Documentation