πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
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.
Use eslint-disable comment if you really need to use it or disable the rule entirely if you prefer functional programming.
This rule is not fixable.
// β
array.reduce(reducer);
// β
// eslint-disable-next-line unicorn/no-array-reduce
array.reduce(reducer);// β
array.reduce(reducer, initialValue);
// β
let result = initialValue;
for (const element of array) {
result += element;
}// β
array.reduce((total, value) => total + value);// β
array.reduceRight(reducer, initialValue);
// β
let result = initialValue;
for (const element of array.toReversed()) { // Equivalent to .reduceRight()
result += element;
}// β
[].reduce.call(array, reducer);// β
[].reduce.apply(array, [reducer, initialValue]);// β
Array.prototype.reduce.call(array, reducer);Type: boolean
Default: true
Allow simple operations (like addition, subtraction, etc.) in a reduce call.
Set it to false to disable reduce completely.
/* eslint unicorn/no-array-reduce: ["error", {"allowSimpleOperations": true}] */
// β
array.reduce((total, item) => total + item)/* eslint unicorn/no-array-reduce: ["error", {"allowSimpleOperations": false}] */
// β
array.reduce((total, item) => total + item)