ecmascript-6 - 如何满足 lint 规则 'array-callback-return' ?

标签 ecmascript-6 eslint

这是我的第一次剪辑:

const planLimits = {plan1: {condition1: 50, ...}}

function initialisePlanLimits(planLimits) {
  const limits = new Map();
  Object.keys(planLimits).map((planId) => (
    const limitMap = new Map(Object.entries(planLimits[planId]));
    limits.set(planId, limitMap);
  ));
  return limits;
}

linter 会标记此错误:error Expected to return a value in this function array-callback-return
所以我改成了这个版本:
function initialisePlanLimits(planLimits) {
  const limits = new Map();
  Object.keys(planLimits).map((planId) => (
    limits.set(planId, new Map(Object.entries(planLimits[planId])))
  ));
  return limits;
}

它抛出另一个错误 Unexpected parentheses around single function argument having a body with no curly braces arrow-parens
我的问题:

1) 我认为我可以通过粘贴 return null 来修复我的第一个版本在 curry 括号内。但是有没有更好、更优雅的方式呢?在这种情况下,虚假的 return 语句没有意义

2)为什么第二个版本失败?不是和第一个版本一样吗?

最佳答案

如果我使用 forEach而不是 map ,不会引起array-callback-return Lint 错误

 Object.keys(planLimits).forEach((planId) => (
    const limitMap = new Map(Object.entries(planLimits[planId]));
    limits.set(planId, limitMap);
  ));

关于ecmascript-6 - 如何满足 lint 规则 'array-callback-return' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43022558/

相关文章:

javascript - 如何配置 ES6 代码检查

javascript - 带有异步返回 Promise 的 Map

javascript - JS : How to change the value of an object's property according to that object attribute

javascript - 如果输入为空,我该怎么做,什么都不会发生

javascript - 无法在 Atom 编辑器中使用 eslint

eslint - 为什么会在这里找到 require-atomic-updates ?

javascript - 如何将 typescript 项目与 html 集成

javascript - ESLint:防止在 Javascript 中使用箭头函数(我从小就称它们为 Lambda 函数)

eslint - 抑制特定的 ESLint 警告 : File ignored by default

javascript - ESLint:为什么 Symbol 没有定义(no-undef 规则)?