no-return-assign - 何时禁用

时间:2017-12-18 23:19:43

标签: javascript object foreach eslint

我正在尝试在此对象数组中传递id:

const children = [{name: Jack, age: 8}, {name: Joe, age: 6}];
children.forEach((child) => (child.id = v4()));

我一直得到eslint错误no-return-assign这真是让我烦恼(双关语)

我试过了:

children.forEach(child => child.id = v4());

children.forEach((child) => (child.id = v4())); 

children.forEach(child => (child.id = v4()));

children.forEach((child) => {
    return child.id = v4()
});

没有工作。

我应该禁用eslint吗?有解决方法吗?

1 个答案:

答案 0 :(得分:0)

不要从回调函数中返回任何内容。只需写下

children.forEach(child => {
    child.id = v4();
});

甚至更简单

for (const child of children) {
    child.id = v4();
}