Skip to content

Latest commit

 

History

History
48 lines (34 loc) · 938 Bytes

no-array-reduce.md

File metadata and controls

48 lines (34 loc) · 938 Bytes

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

Array#reduce() and Array#reduceRight() usually result in hard-to-read 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.

Use eslint-disable comment if you really need to use it.

This rule is not fixable.

Fail

array.reduce(reducer, initialValue);
array.reduceRight(reducer, initialValue);
array.reduce(reducer);
[].reduce.call(array, reducer);
[].reduce.apply(array, [reducer, initialValue]);
Array.prototype.reduce.call(array, reducer);

Pass

// eslint-disable-next-line unicorn/no-array-reduce
array.reduce(reducer, initialValue);
let result = initialValue;

for (const element of array) {
	result += element;
}