如何满足皮棉规则"array-callback-return"? [英] How to satisfy the lint rules 'array-callback-return'?

查看:186
本文介绍了如何满足皮棉规则"array-callback-return"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个切菜:

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;
}

短绒棉衣标记此错误:error Expected to return a value in this function array-callback-return

The linter flags this error: error Expected to return a value in this function array-callback-return

所以我改成了这个版本:

So I changed to this version:

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粘贴在咖喱括号内来修复我的第一个版本.但是,有没有更好,更优雅的方法呢?在这种情况下,伪造的return语句没有任何意义

1) I reckon I can fix my first version by sticking in a return null within the curry bracket. But is there a better, more elegant way? A bogus return statement does not make sense in this context

2)为什么第二个版本失败?它不等于第一个版本吗?

2) Why the second version fails? Isn't it equivalent to the first version?

推荐答案

如果我使用forEach而不是map,则不会导致array-callback-return棉绒错误

If I use forEach instead of map, it will not cause the array-callback-return lint error

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

这篇关于如何满足皮棉规则"array-callback-return"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆