Skip to content

Prefer .flatMap(…) over .map(…).flat()

💼 This rule is enabled in the ✅ recommended config.

🔧 This rule is automatically fixable by the --fix CLI option.

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

Fail

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

Pass

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

Released under the Apache License 2.0.