gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:35:11 25 4
gpt4 key购买 nike

我使用以下代码按预期工作,同时使用 ESLINT
我有错误

ESLint: iterators/generators require regenerator-runtime, which is tooheavyweight for this guide to allow them. Separately, loops should beavoided 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 beleaving it behind.

    array.map has functional connotations and we shouldn't be producingside effects in the closure.

    array.forEach is an option, but I personally don't like it for thissort of imperative work.

    So I think the ForOfStatement should be removed from the restrictedsyntax 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com