Skip to content
← Back to rules

unicorn/no-array-reduce Restriction

它的作用

禁止使用 Array#reduce()Array#reduceRight()

为什么这不好?

Array#reduce()Array#reduceRight() 通常会生成难以阅读性能较差的代码。在几乎所有情况下,它都可以被 .map.filterfor-of 循环替代。

它只有在罕见的数字求和场景中才有一定用处,而这默认是允许的。

示例

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

javascript
array.reduce(reducer, initialValue);
array.reduceRight(reducer, initialValue);

配置

此规则接受一个包含以下属性的配置对象:

allowSimpleOperations

type: boolean

default: true

设置为 true 时,允许在 reducereduceRight 调用中使用简单操作(例如数字求和)。

如何使用

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

json
{
  "rules": {
    "unicorn/no-array-reduce": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则在 v0.0.19 中加入。

参考