Skip to content

Latest commit

Β 

History

History
61 lines (42 loc) Β· 2.62 KB

prefer-array-flat-map.md

File metadata and controls

61 lines (42 loc) Β· 2.62 KB

prefer-array-flat-map

πŸ“ Prefer .flatMap(…) over .map(…).flat() and .filter(…).flatMap(…).

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§πŸ’‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

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

It expresses mapping and flattening directly and avoids intermediate arrays created by separate map() or filter() calls.

It can also add or remove items during mapping by returning an empty array for items that should be skipped. This rule only reports .filter().flatMap() when the .flatMap() callback can return multiple items. Single-item callbacks are handled by unicorn/no-unnecessary-array-flat-map.

flatMap() flattens each callback result immediately, while map().flat() waits until all callbacks finish, so the results can differ if a callback mutates an array returned by an earlier callback. The rule autofixes inline arrow functions that return an array literal or an expression without side effects, like value => value.children, and offers a suggestion for other callbacks.

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
	.filter(element => element.isUnicorn)
	.flatMap(element => [element.name, element.alias]);

// βœ…
const foo = bar.flatMap(element => element.isUnicorn ? [element.name, element.alias] : []);
// βœ…
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