Skip to content
← Back to rules

unicorn/prefer-array-flat-map Perf

🛠️ An auto-fix is available for this rule.

它的作用

.map().flat() 一起使用时,优先使用 .flatMap()

为什么这有问题?

使用 .flatMap(…) 而不是 .map(…).flat() 会稍微更高效一些。

示例

以下是此规则的错误代码示例:

javascript
const bar = [1, 2, 3].map((i) => [i]).flat();

以下是此规则的正确代码示例:

javascript
const bar = [1, 2, 3].flatMap((i) => [i]);

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/prefer-array-flat-map": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-array-flat-map": "error",
  },
});
bash
oxlint --deny unicorn/prefer-array-flat-map

版本

此规则是在 v0.0.14 中添加的。

参考