eslint-plugin-no-array-reduce

ESLint rule to disallow Array.reduce() method.

Usage no npm install needed!

<script type="module">
  import eslintPluginNoArrayReduce from 'https://cdn.skypack.dev/eslint-plugin-no-array-reduce';
</script>

README

eslint-plugin-no-array-reduce

npm version npm downloads CI semantic-release TypeScript

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:

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' },
];

// Filter dairy products
const dairy = groceries.reduce((acc, grocery) => (grocery.type === 'dairy' ? acc.concat(grocery) : acc), []);

Pass

// Filter dairy products
const dairy = groceries.filter((grocery) => grocery.type === 'dairy');

Contributing

All contributions are welcome!