Disable the unicorn/no-array-for-each
rule
#228
Replies: 2 comments
-
Benefits of for…of statement over the
Additionally, using Please see the documentation of the rule. |
Beta Was this translation helpful? Give feedback.
-
I would only consider the const arr = [1, 2, 3, 4];
// same
arr.forEach(item => {
console.log(item);
})
//vs
for (let item of arr) {
console.log(item);
}
// slightly different
array.forEach((element, index) => {
bar(element, index);
});
// vs
for (const [index, element] of array.entries()) {
bar(element, index);
} That small win is not enough for me to throw away all the benefits that You can even still write a forEach, eslint will automatically convert it for you :) IMO the only valid use case (but feel free to add more) for forEach is when chaining after other array methods to do something with the created result: // I prefer this
list
.map(x => modifyX(x))
.filter(x => x > 5)
.forEach(x => executeX(x));
// but not this
[1, 2, 5].forEach((x) => executeX(x)); I'm not happy how eslint auto-fixes the first variant though :( // horrible
for (const x of [1, 2, 5].map((x) => modifyX(x)).filter((x) => x > 5)) {
executeX(x);
}
// much better
for (const x of [1, 2, 5]) {
executeX(x);
} |
Beta Was this translation helpful? Give feedback.
-
There is a proposal to disable the
unicorn/no-array-for-each
rule.Beta Was this translation helpful? Give feedback.
All reactions