Skip to content
← Back to rules

unicorn/no-array-reduce Restriction

🚧 An auto-fix is planned for this rule, but not implemented at this time.

What it does ​

Disallow Array#reduce() and Array#reduceRight().

Why is this bad? ​

Array#reduce() and Array#reduceRight() usually result in hard-to-read and less performant code. In almost every case, it can be replaced by .map, .filter, or a for-of loop.

It's only somewhat useful in the rare case of summing up numbers, which is allowed by default.

Examples ​

Examples of incorrect code for this rule:

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

Configuration ​

This rule accepts a configuration object with the following properties:

allowSimpleOperations ​

type: boolean

default: true

When set to true, allows simple operations (like summing numbers) in reduce and reduceRight calls.

How to use ​

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

Version ​

This rule was added in v0.0.19.

References ​