Skip to content

Latest commit

Β 

History

History
44 lines (30 loc) Β· 1.35 KB

File metadata and controls

44 lines (30 loc) Β· 1.35 KB

prefer-array-flat-map

πŸ“ 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));

Related rules