【问题标题】:no-return-assign / no-unused-expressionsno-return-assign / no-unused-expressions
【发布时间】:2021-06-17 08:42:44
【问题描述】:

我的代码中有以下代码行,但 eslint 让我返回一个错误。

this.data.forEach(el => el.value === newValue ? el[column] = newValue[column] : el)

这给了我以下错误: no-return-assign: Arrow function should not return assignment.

this question 中,它声明我将通过简单地将 => 之后的所有内容括在花括号中来解决问题,如下所示:

this.data.forEach(el => { el.value === newValue ? el[column] = newValue[column] : el })

但是,这现在会导致以下错误: no-unused-expression: Expected an assignment or function call and instead saw an expression.

关于如何解决这个问题的任何线索?

【问题讨论】:

    标签: javascript vue.js eslint


    【解决方案1】:

    您收到此类警告的原因是因为将命令式代码放在表达式中会造成混淆。您的代码相当于这样的代码,可读性更强:

    this.data.forEach(el => {
        if (el.value === newValue) {
            el[column] = newValue[column];
            return newValue[column];
        else {
            return el;
        }
    });
    

    值得注意的是,forEach 中回调的返回值被忽略了,因此您的代码实际上可能与您的预期有所不同。如果赋值语句是你想要的,你可以这样做:

    this.data
        .filter(el => el.value === newValue)
        .forEach(el => {
            el[column] = newValue[column];
        });
    

    【讨论】:

      猜你喜欢
      • 2020-04-09
      • 2020-05-25
      • 2019-03-28
      • 1970-01-01
      • 2016-09-30
      • 2019-03-31
      • 1970-01-01
      • 2019-12-22
      相关资源
      最近更新 更多