no-return-assign / no-unused-expressions
no-return-assign / no-unused-expressions
我的代码中有以下代码行,但 eslint 返回错误。
this.data.forEach(el => el.value === newValue ? el[column] = newValue[column] : el)
这给了我以下错误:
no-return-assign: Arrow function should not return assignment.
在 中,它指出我将通过简单地将 => 之后的所有内容括在大括号中来解决问题,如下所示:
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.
关于如何解决这个问题的任何线索?
您收到此类警告的原因是将命令式代码放入表达式中会造成混淆。你的代码等同于这样的东西,它更具可读性:
this.data.forEach(el => {
if (el.value === newValue) {
el[column] = newValue[column];
return newValue[column];
else {
return el;
}
});
值得注意的是,forEach 中回调的 return 值被忽略了,因此您的代码实际上可能做了一些与您预期不同的事情。如果赋值语句就是你想要的,你可以这样做:
this.data
.filter(el => el.value === newValue)
.forEach(el => {
el[column] = newValue[column];
});
我的代码中有以下代码行,但 eslint 返回错误。
this.data.forEach(el => el.value === newValue ? el[column] = newValue[column] : el)
这给了我以下错误:
no-return-assign: Arrow function should not return assignment.
在
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.
关于如何解决这个问题的任何线索?
您收到此类警告的原因是将命令式代码放入表达式中会造成混淆。你的代码等同于这样的东西,它更具可读性:
this.data.forEach(el => {
if (el.value === newValue) {
el[column] = newValue[column];
return newValue[column];
else {
return el;
}
});
值得注意的是,forEach 中回调的 return 值被忽略了,因此您的代码实际上可能做了一些与您预期不同的事情。如果赋值语句就是你想要的,你可以这样做:
this.data
.filter(el => el.value === newValue)
.forEach(el => {
el[column] = newValue[column];
});