eslint-plugin-no-array-reduce
![TypeScript](https://badges.frapsoft.com/typescript/code/typescript.svg?v=101)
ESLint rule to disallow Array.reduce()
method.
Method reduce()
in most cases can be written as map()
, filter()
or one of the for()
loops which benefits in code readability and makes it easier to maintain for future developers.
Subjectively there are cases where you still might want to use reduce()
with eslint-disable
.
There are many debates, discussions and other resources related to it:
![](https://github.com/mkosir/eslint-plugin-no-array-reduce/raw/HEAD/code.png)
Install
npm install --save-dev eslint-plugin-no-array-reduce
Then extend eslint config:
{
"extends": [
"plugin:no-array-reduce/recommended"
]
}
Fail
const groceries = [
{ name: 'milk', type: 'dairy' },
{ name: 'cheese', type: 'dairy' },
{ name: 'beef', type: 'meat' },
];
const dairy = groceries.reduce((acc, grocery) => (grocery.type === 'dairy' ? acc.concat(grocery) : acc), []);
Pass
const dairy = groceries.filter((grocery) => grocery.type === 'dairy');
Contributing
All contributions are welcome!