# unicorn/prefer-array-find

> Require `.find()` or `.findLast()` instead of taking one element from `.filter()`

`.filter()` tests every element and builds a new array. If you only take the first or the last match from it, the rest of that work is thrown away. `.find()` and `.findLast()` stop at the match and return it.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/prefer-array-find" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 5} */}
```js
const admin = users.filter(user => user.isAdmin)[0];
const first = users.filter(user => user.isActive).shift();
const [owner] = users.filter(user => user.isOwner);
const newest = users.filter(user => user.isActive).at(-1);
const lastGuest = users.filter(user => user.isGuest).pop();
```

Not reported:

{/* good */}
```js
const admin = users.find(user => user.isAdmin);
const first = users.find(user => user.isActive);
const owner = users.find(user => user.isOwner);
const newest = users.findLast(user => user.isActive);
const lastGuest = users.findLast(user => user.isGuest);
```

A variable that holds the result of `.filter()` is reported too, if you only read its first element:

{/* bad */}
```js
const admins = users.filter(user => user.isAdmin);
notify(admins[0]);
```

If you use the variable in any other way, it's fine:

{/* good */}
```js
const admins = users.filter(user => user.isAdmin);
notify(admins[0]);
console.log(admins.length);
```
