Skip to content

noArrayReduce (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noArrayReduce": "error"
}
}
}
}

Disallow the reduce() and reduceRight() array methods.

The function passed to reduce() receives the result of its previous call along with the next item, and it has to return the result for the following call. This makes it hard to see what the code computes, especially when the result is an object or an array that is built up step by step. A for...of loop, or methods such as map() and filter(), usually express the same thing more clearly.

This rule also reports reduce() and reduceRight() when they are borrowed from Array.prototype or [] with call() or apply().

Calls whose function only returns the result of a single arithmetic, comparison, or bitwise operation, such as (total, item) => total + item, are allowed, because they are short and easy to read. Logical operations, such as (found, item) => found || item, are still reported.

This rule doesn’t report calls when the value is known not to be an array, such as a Map or a Set, either because it is created with new Map() or because its type annotation says so.

const counts = words.reduce((counts, word) => {
counts[word] = (counts[word] ?? 0) + 1;
return counts;
}, {});
code-block.js:1:22 lint/nursery/noArrayReduce ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Avoid calling reduce().

> 1 │ const counts = words.reduce((counts, word) => {
│ ^^^^^^
2 │ counts[word] = (counts[word] ?? 0) + 1;
3 │ return counts;

ℹ The function passed to reduce() has to pass its result along to the next call, which makes it hard to see what the code computes.

ℹ Use a for...of loop, or methods such as map() and filter(), instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const result = array.reduceRight(reducer, initialValue);
code-block.js:1:22 lint/nursery/noArrayReduce ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Avoid calling reduceRight().

> 1 │ const result = array.reduceRight(reducer, initialValue);
│ ^^^^^^^^^^^
2 │

ℹ The function passed to reduceRight() has to pass its result along to the next call, which makes it hard to see what the code computes.

ℹ Use a for...of loop instead. To go through the items from last to first, loop over toReversed().

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const result = Array.prototype.reduce.call(array, reducer);
code-block.js:1:32 lint/nursery/noArrayReduce ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Avoid calling reduce().

> 1 │ const result = Array.prototype.reduce.call(array, reducer);
│ ^^^^^^
2 │

ℹ The function passed to reduce() has to pass its result along to the next call, which makes it hard to see what the code computes.

ℹ Use a for...of loop, or methods such as map() and filter(), instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const result = [].reduce.apply(array, [reducer, initialValue]);
code-block.js:1:19 lint/nursery/noArrayReduce ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Avoid calling reduce().

> 1 │ const result = [].reduce.apply(array, [reducer, initialValue]);
│ ^^^^^^
2 │

ℹ The function passed to reduce() has to pass its result along to the next call, which makes it hard to see what the code computes.

ℹ Use a for...of loop, or methods such as map() and filter(), instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const counts = {};
for (const word of words) {
counts[word] = (counts[word] ?? 0) + 1;
}
const total = array.reduce((total, item) => total + item, 0);
const product = array.reduce(function (product, item) {
return product * item;
});