【问题标题】:Warning: Array.prototype.reduce() expects a return value from arrow function警告:Array.prototype.reduce() 需要箭头函数的返回值
【发布时间】:2022-01-23 17:52:26
【问题描述】:
const singleParams = [...appliedFilters].reduce((prev, curr) => {
    if (curr.key !== "multi")
        return { ...prev, ...curr.selectedValue.params };
    return;
}, {});

以下函数在我的控制台中给我以下警告。

第 22:13 行:Array.prototype.reduce() 期望从 箭头函数数组回调返回

我不能忽略它,因为我无法在出现警告的情况下进行部署。我该如何解决?

【问题讨论】:

  • 这是由于return; 语句,尝试返回一个值而不是undefined(比如return prev;
  • 当你使用花括号{}你需要有回报。如果您不希望这样,请使用括号 ()

标签: javascript arrays


【解决方案1】:

您需要在 else 块中返回 prev

  const singleParams = [...appliedFilters].reduce((prev, curr) => {
        if (curr.key !== "multi")
            return { ...prev, ...curr.selectedValue.params };
        return prev;
    }, {});

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 2022-01-08
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多