src/content/docs/linter/rules/use-find.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: :::note This rule belongs to the types domain. This means that its activation will activate the Biome Scanner to scan the files of your project, and enable the type inference engine. Read more about it in the [documentation page](/linter/domains#types) ::: ## Summary - Rule available since: `v2.3.6` - Diagnostic Category: [`lint/nursery/useFind`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - This rule belongs to the following domains: - [`types`](/linter/domains#types) - Sources: - Same as [`@typescript-eslint/prefer-find`](https://typescript-eslint.io/rules/prefer-find){
"linter": {
"rules": {
"nursery": {
"useFind": "error"
}
}
}
}
Enforce the use of Array.prototype.find() over Array.prototype.filter() followed by [0] when looking for a single result.
When searching for the first item in an array matching a condition, it may be tempting to use code like arr.filter(x => x > 0)[0].
However, it is simpler to use Array.prototype.find() instead, arr.find(x => x > 0), which also returns the first entry matching a condition.
Because the .find() only needs to execute the callback until it finds a match, it's also more efficient.
[1, 2, 3].filter(x => x > 1)[0];
[1, 2, 3].filter(x => x > 1).at(0);
[1, 2, 3].find(x => x > 1);