noArrayForEach (JavaScript)
Summary
Section titled βSummaryβ- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noArrayForEach - This rule doesnβt have a fix.
- The default severity of this rule is warning.
- Sources:
- Inspired from
unicorn/no-for-each
- Inspired from
How to configure
Section titled βHow to configureβ{ "linter": { "rules": { "nursery": { "noArrayForEach": "error" } } }}Description
Section titled βDescriptionβDisallow the forEach() method in favor of for...of loops.
A for...of loop runs the same code for each item of an array, and it has several
advantages over passing a function to forEach():
breakstops the loop early, andcontinueskips to the next item.returnandawaitapply to the surrounding function, so you can leave that function or wait for a promise from inside the loop.- In TypeScript, a type checked before the loop is still known inside the loop, because the loop body is not a separate function.
Use array.entries() when you also need the index of each item:
for (const [index, item] of array.entries()).
This rule doesnβt report calls when the value is known not to be an array,
such as a Map or a Set, either because it is created with new Map()
or because its type annotation says so.
It also ignores forEach() from the following libraries,
because they take the list as an argument instead of looping over the value theyβre called on:
React.Children, Children, R (Ramda), pIteration, Effect,
and namespace imports of strict-callbag-basics.
Examples
Section titled βExamplesβInvalid
Section titled βInvalidβarray.forEach((element) => { bar(element);});code-block.js:1:7 lint/nursery/noArrayForEach ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β This forEach() call can be written as a for...of loop.
> 1 β array.forEach((element) => {
β ^^^^^^^
2 β bar(element);
3 β });
βΉ A for...of loop can use break and continue, and return and await inside it apply to the surrounding function.
βΉ Replace this call with a for...of loop. Loop over entries() if you also need the index.
βΉ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
array?.forEach((element) => { bar(element);});code-block.js:1:8 lint/nursery/noArrayForEach ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β This forEach() call can be written as a for...of loop.
> 1 β array?.forEach((element) => {
β ^^^^^^^
2 β bar(element);
3 β });
βΉ A for...of loop can use break and continue, and return and await inside it apply to the surrounding function.
βΉ Replace this call with a for...of loop. Loop over entries() if you also need the index.
βΉ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
array.forEach((element, index) => { bar(element, index);});code-block.js:1:7 lint/nursery/noArrayForEach ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β This forEach() call can be written as a for...of loop.
> 1 β array.forEach((element, index) => {
β ^^^^^^^
2 β bar(element, index);
3 β });
βΉ A for...of loop can use break and continue, and return and await inside it apply to the surrounding function.
βΉ Replace this call with a for...of loop. Loop over entries() if you also need the index.
βΉ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
for (const element of array) { bar(element);}
for (const [index, element] of array.entries()) { bar(element, index);}
const map = new Map();map.forEach((value, key) => bar(key, value));
function log(set: Set<string>) { set.forEach((value) => console.log(value));}See Also
Section titled βSee AlsoβnoForEachalso reportsforEach()calls, but allows callbacks that use the index of each item.
Related links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.