【问题标题】:Stuck with eslint Error i.e Separately, loops should be avoided in favor of array iterations陷入 eslint 错误,即单独地,应避免循环以支持数组迭代
【发布时间】:2019-09-14 14:38:29
【问题描述】:

我有一些迭代的代码,它运行良好。安装 eslint 后,我​​的代码之一由 eslint 生成错误。

我的代码是:

for (const column of columns) {
    for (const slugname of result[column.name]) {
        const alphabet = slugname.slugname;
        if (total[alphabet]) {
            total[alphabet] += column.value;
        } else {
            total[alphabet] = column.value;
        }
    }
}

eslint 会生成一个错误

error 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

非常感谢任何帮助或建议。据我说代码写得很精确,很小,不知道eslint错误的线索

【问题讨论】:

  • 你试过不使用for ... of吗?
  • 否,因为 for 会比 foreach 处理得更好
  • array.find() 会解决这个问题吗? link
  • 考虑使用.map()?

标签: javascript ecmascript-6 eslint eslint-config-airbnb


【解决方案1】:
columns.map(x => result[x.name].map((y) => {
  const alphabet = y.slugname;
  if (total[alphabet]) {
      total[alphabet] += x.value;
    } else {
      total[alphabet] = x.value;
    }
    return true;
}));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    相关资源
    最近更新 更多