Skip to content

Latest commit

 

History

History
42 lines (28 loc) · 1.27 KB

prefer-array-flat-map.md

File metadata and controls

42 lines (28 loc) · 1.27 KB

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

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

Pass

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));

Related rules