Skip to content

noFlatMapIdentity

Diagnostic Category: lint/correctness/noFlatMapIdentity

Since: v1.7.0

Sources:

Disallow to use unnecessary callback on flatMap.

To achieve the same result (flattening an array) more concisely and efficiently, you should use flat instead.

array.flatMap((arr) => arr);
code-block.js:1:1 lint/correctness/noFlatMapIdentity  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid unnecessary callback in flatMap call.

> 1 │ array.flatMap((arr) => arr);
^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

You can just use flat to flatten the array.

Safe fix: Replace unnecessary flatMap call to flat instead.

1 - array.flatMap((arr)·=>·arr);
1+ array.flat();
2 2

array.flatMap((arr) => {return arr});
code-block.js:1:1 lint/correctness/noFlatMapIdentity  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid unnecessary callback in flatMap call.

> 1 │ array.flatMap((arr) => {return arr});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

You can just use flat to flatten the array.

Safe fix: Replace unnecessary flatMap call to flat instead.

1 - array.flatMap((arr)·=>·{return·arr});
1+ array.flat();
2 2

array.flatMap((arr) => arr * 2);