简体   繁体   中英

no-return-assign - when to disable

I am trying to pass ids in this object array:

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

I keep getting eslint error no-return-assign this is really bugging me (pun intended)

I tried:

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()
});

None work.

Should I disable eslint? Is there a workaround?

Well don't return anything from your callback function. Just write

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

or even simpler

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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