Skip to content

unicorn/no-array-reduce Restriction

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 in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/no-array-reduce
json
{
  "rules": {
    "unicorn/no-array-reduce": "error"
  }
}

References

Released under the MIT License.