unicorn/no-for-each
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/no-for-each": "error",
},
},
];array.forEach(element => {
bar(element);
});array?.forEach(element => {
bar(element);
});array.forEach((element, index) => {
bar(element, index);
});for (const element of array) {
bar(element);
}if (array) {
for (const element of array) {
bar(element);
}
}for (const [index, element] of array.entries()) {
bar(element, index);
}Examples from the ESLint documentation.
unicorn/no-for-each
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/no-for-each": "error",
},
},
];Oxlint does not have this rule.
nursery/noArrayForEach
nursery · since 2.6.5
{
"linter": {
"rules": {
"nursery": {
"noArrayForEach": "error"
}
}
}
}Disallow two comparisons of the same operands that can be combined into one.
Disallow negated array predicate calls.
Disallow boolean-returning sort comparators.
Prefer comparing values directly over subtracting and comparing to 0.
Disallow sorting arrays to get the minimum or maximum value.
Disallow unnecessary nested ternary expressions.