Skip to content
You are currently reading docs for v1.x. Go to the latest docs if you are using newer version.

useFlatMap

Diagnostic Category: lint/complexity/useFlatMap

Since: v1.0.0

Sources:

Promotes the use of .flatMap() when map().flat() are used together.

const array = ["split", "the text", "into words"];
array.map(sentence => sentence.split(' ')).flat();
code-block.js:2:1 lint/complexity/useFlatMap  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ The call chain .map().flat() can be replaced with a single .flatMap() call.

1 │ const array = [“split”, “the text”, “into words”];
> 2 │ array.map(sentence => sentence.split(’ ‘)).flat();
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │

ℹ Safe fix: Replace the chain with .flatMap().

1 1 │ const array = [“split”, “the text”, “into words”];
2 │ - array.map(sentence·=>·sentence.split(’·‘)).flat();
2 │ + array.flatMap(sentence·=>·sentence.split(’·’));
3 3 │

const array = ["split", "the text", "into words"];
array.map(sentence => sentence.split(' ')).flat(1);
code-block.js:2:1 lint/complexity/useFlatMap  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ The call chain .map().flat() can be replaced with a single .flatMap() call.

1 │ const array = [“split”, “the text”, “into words”];
> 2 │ array.map(sentence => sentence.split(’ ‘)).flat(1);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │

ℹ Safe fix: Replace the chain with .flatMap().

1 1 │ const array = [“split”, “the text”, “into words”];
2 │ - array.map(sentence·=>·sentence.split(’·‘)).flat(1);
2 │ + array.flatMap(sentence·=>·sentence.split(’·’));
3 3 │

const array = ["split", "the text", "into words"];
array.map(sentence => sentence.split(' ')).flat(2);
biome.json
{
"linter": {
"rules": {
"complexity": {
"useFlatMap": "error"
}
}
}
}