javascript - 使用 for 循环为数组提供错误

标签 javascript node.js for-loop

我使用以下代码按预期工作,同时使用 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;
  }
}
代码很简单
  • 从其他函数获取数据数组
  • 在每个数组项上循环
  • 当第一个数组实例有 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..offorEach 贵, 检查 this出去
    我没有意见,你可以删除 eslint 规则

    关于javascript - 使用 for 循环为数组提供错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63241443/

    相关文章:

    node.js - Typescript2 路径模块解析

    java - 在计算字符串中的单词数时如何考虑空格?

    javascript - 从对象和数组创建对象数组

    javascript - 有人能发现问题吗?只有第一个选项适用于选择

    javascript - Bootstrap 4 : switch between cols on mobile

    node.js - 如何在 NodeJS REPL 中使用 "reverse interactive search"?

    node.js - 在 Nodejs Express 中运行 https 服务器时出现 ERR_SSL_PROTOCOL_ERROR 浏览器错误消息

    JavaScript - 带 switch 语句的 For 循环不断循环

    javascript - 电话号码验证问题

    javascript - vue.js:手动触发绑定(bind)更新