# prefer-iterator-to-array-at-end

Rule `unicorn/prefer-iterator-to-array-at-end`. Prefer moving `.toArray()` to the end of iterator helper chains.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #981 by reach, #1236 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-iterator-to-array-at-end` | no | suggestion | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-iterator-to-array-at-end.md) |

## Code examples

### Incorrect: the rule reports this

```js
const result = iterator.toArray().map(element => element * 2);
```

```js
const result = iterator.toArray().filter(element => element > 1);
```

```js
const result = iterator.toArray().flatMap(element => [element, element]);
```

### Correct: the rule accepts this

```js
const result = iterator.map(element => element * 2).toArray();
```

```js
const result = iterator.filter(element => element > 1).toArray();
```

```js
const result = iterator.toArray().map(callback);
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-iterator-to-array-at-end": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-iterator-helpers](https://lintradar.dev/rules/unicorn/prefer-iterator-helpers.md): Prefer iterator helpers over temporary arrays from iterators.
- [unicorn/prefer-literal-ascii](https://lintradar.dev/rules/unicorn/prefer-literal-ascii.md): Prefer literal printable ASCII characters over escape sequences.
- [unicorn/prefer-iterable-in-constructor](https://lintradar.dev/rules/unicorn/prefer-iterable-in-constructor.md): Prefer passing iterables directly to constructors instead of filling empty collections.
- [unicorn/prefer-map-from-entries](https://lintradar.dev/rules/unicorn/prefer-map-from-entries.md): Prefer `new Map()` over `Object.fromEntries()` when using the result as a map.
- [unicorn/prefer-identifier-import-export-specifiers](https://lintradar.dev/rules/unicorn/prefer-identifier-import-export-specifiers.md): Prefer identifiers over string literals in import and export specifiers.
- [unicorn/prefer-minimal-ternary](https://lintradar.dev/rules/unicorn/prefer-minimal-ternary.md): Prefer moving ternaries into the minimal varying part of an expression.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-iterator-to-array-at-end. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
