简体   繁体   English

no-return-assign / no-unused-expressions

[英]no-return-assign / no-unused-expressions

I have the following line of code working on my code, but eslint is getting me an error back.我有以下代码行在我的代码上工作,但 eslint 让我返回一个错误。

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

This gives me the following error: no-return-assign: Arrow function should not return assignment.这给了我以下错误: no-return-assign: Arrow function should not return assignment.

In this question it states I would solve the problem by simply surrounding everything after => in curly braces like this:这个问题中,它指出我将通过简单地将 => 之后的所有内容括在花括号中来解决问题,如下所示:

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

However, this is now causing the following error: no-unused-expression: Expected an assignment or function call and instead saw an expression.但是,这现在导致以下错误: no-unused-expression: Expected an assignment or function call and instead saw an expression.

Any clues on how to solve this?关于如何解决这个问题的任何线索?

The reason you're getting these kinds of warnings is because it's confusing to put imperative code inside an expression.您收到此类警告的原因是因为将命令式代码放在表达式中会令人困惑。 Your code is equivalent to something like this, which is much more readable:您的代码相当于这样的代码,更具可读性:

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

It's worth noting that the return value of a callback in forEach is ignored, so your code actually probably does something different to what you intended.值得注意的是,forEach 中回调的返回值被忽略了,因此您的代码实际上可能与您的预期有所不同。 If the assignment statement is all you wanted, you can do this:如果赋值语句是你想要的,你可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM