# no-unnecessary-array-flat-map πŸ“ Disallow `Array#flatMap()` callbacks that only wrap a single item. πŸ’ΌπŸš« This rule is enabled in the βœ… `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). This rule is _disabled_ in the β˜‘οΈ `unopinionated` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). πŸ”§πŸ’‘ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix) and manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). [`Array#flatMap()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap) is useful when one input item can become multiple output items. When a callback only returns `[item]` or `condition ? [item] : []`, `.map()`, `.filter()`, or `.filter().map()` is clearer. This rule currently checks simple arrow callbacks that return either a one-item array or `condition ? [item] : []`. More complex callback bodies are intentionally ignored. ## Examples ```js // ❌ const ids = array.flatMap(value => [value.id]); // βœ… const ids = array.map(value => value.id); ``` ```js // ❌ const ids = array.filter(value => value.active).flatMap(value => [value.id]); // βœ… const ids = array.filter(value => value.active).map(value => value.id); ``` ```js // ❌ const active = array.flatMap(value => value.active ? [value] : []); // βœ… const active = array.filter(value => value.active); ``` ```js // ❌ const ids = array.flatMap(value => value.active ? [value.id] : []); // βœ… const ids = array.filter(value => value.active).map(value => value.id); ``` ```js // βœ… const descendants = array.flatMap(value => value.children); ``` ```js // βœ… const values = array.flatMap(value => [value, value * 2]); ``` ## Related rules - [unicorn/prefer-array-flat-map](./prefer-array-flat-map.md)