prefer-array-flat-map

March 27, 2026 ยท View on GitHub

๐Ÿ“ Prefer .flatMap(โ€ฆ) over .map(โ€ฆ).flat().

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

Array#flatMap performs Array#map and Array#flat in one step.

Examples

// โŒ
const foo = bar.map(element => unicorn(element)).flat();

// โŒ
const foo = bar.map(element => unicorn(element)).flat(1);

// โœ…
const foo = bar.flatMap(element => unicorn(element));
// โœ…
const foo = bar.map(element => unicorn(element)).flat(2);
// โœ…
const foo = bar.map(element => unicorn(element)).foo().flat();
// โœ…
const foo = bar.flat().map(element => unicorn(element));