sfl*_*che 25 javascript ecmascript-6 eslint
我最近添加了eslint规则no-param-reassign.
但是,当我reduce用来构建一个对象(空对象initialValue)时,我发现自己需要accumulator在每次回调迭代时修改(回调函数的第一个arg),这会引起一个no-param-reassignlinter投诉(正如人们所期望的那样).
const newObject = ['a', 'b', 'c'].reduce((result, item, index) => {
result[item] = index; // <-- causes the no-param-reassign complaint
return result;
}, {});
Run Code Online (Sandbox Code Playgroud)
是否有更好的方法来构建一个reduce不修改accumulator参数的对象?
或者我应该简单地在reduce回调函数中禁用该行的linting规则?
Anj*_*Anj 33
每当我使用时,Array.prototype.reduce我总是命名“accumulator”参数accu。这个约定让我可以方便地设置我的 eslint 规则:
"no-param-reassign": [
"error",
{
"props": true,
"ignorePropertyModificationsFor": ["accu"]
}
],
Run Code Online (Sandbox Code Playgroud)
如果您对此参数有自己的命名约定,请将上面规则中的“accu”替换为您使用的任何内容,并且 eslint 不会抱怨修改您的累加器。
Zac*_*opp 19
我只是将 reduce 函数包装在一个 lint 规则禁用块中,即:
/* eslint-disable no-param-reassign */
const newObject = ['a', 'b', 'c'].reduce((result, item, index) => {
result[item] = index;
return result;
}, {});
/* eslint-enable no-param-reassign */
Run Code Online (Sandbox Code Playgroud)
sfl*_*che 17
我正在重新审视这个问题,以便使用对象扩展运算符发布最终发生的新答案)...
const newObject = ['a', 'b', 'c'].reduce((result, item, index) => ({
...result,
[item]: index,
}), {});
Run Code Online (Sandbox Code Playgroud)
好吧,你可以(result, item) => Object.assign({}, result, {[item]: whatever})在每次迭代时创建一个新对象:-)
如果你想欺骗linter,你可以使用=> Object.assign(result, {[item]: whatever})(它与你当前的代码相同但没有明确的赋值),但是我想你应该简单地禁用该规则.