我使用以下代码按预期工作,同时使用 ESLINT
我有错误
ESLint: iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations. (no-restricted-syntax)
这是代码
for (const subscription of resp.data.subscriptions) {
if (subscription.url) {
return subscription.url;
}
}
代码很简单有没有办法更好地编写它以避免 eslint 问题?
最佳答案
有一个关于for...of
的争论用法here及其最终的限制
for(let i = 0; i < array.length; i ++) { ... }
is antiquated syntax, and while I know everyone understands what it means, we should be leaving it behind.
array.map
has functional connotations and we shouldn't be producing side effects in the closure.
array.forEach
is an option, but I personally don't like it for this sort of imperative work.So I think the ForOfStatement should be removed from the restricted syntax for the above reasons - anyone with any conflicting viewpoints? Do we know what the original justification is?
for..of
比 forEach
贵, 检查 this出去我没有意见,你可以删除 eslint 规则
关于javascript - 使用 for 循环为数组提供错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63241443/