admin管理员组文章数量:1396290
This code runs correctly (converting an array of objects into an object of objects)
ES-lint however gives this error:
[eslint] Arrow function should not return assignment. (no-return-assign)
Please how may this be re-written to satisfy es-lint
var x = arr.reduce((obj, item) => (obj[item.userid] = item, obj), {})
This code runs correctly (converting an array of objects into an object of objects)
ES-lint however gives this error:
[eslint] Arrow function should not return assignment. (no-return-assign)
Please how may this be re-written to satisfy es-lint
var x = arr.reduce((obj, item) => (obj[item.userid] = item, obj), {})
Share
Improve this question
asked Aug 14, 2018 at 1:03
Charles OkwuagwuCharles Okwuagwu
10.9k21 gold badges94 silver badges168 bronze badges
1
- 2 This seems like an ESLint bug. It's not returning the assignment, because of the ma operator. – Barmar Commented Aug 14, 2018 at 1:51
2 Answers
Reset to default 4Personally I like code to be a bit more verbose, because one-liners look very clever today, but next week when I have to e back and fix a bug in that very same place, it will take me a while to understand what I was doing, not to mention if it's someone else who has to fix it.
This is how I would write it:
var x = arr.reduce((obj, item) => {
obj[item.userid] = item;
return obj;
}, {});
Here you have a snippet with some dummy data to test it.
var arr = [
{userid: 11},
{userid: 12},
{userid: 13},
{userid: 14},
];
var x = arr.reduce((obj, item) => {
obj[item.userid] = item;
return obj;
}, {});
console.log(x);
you can use Object.assign
var x = arr.reduce((obj, item) => Object.assign(obj, {[item. userid]: item}), {})
本文标签: javascripthow to fix Eslint quotnoreturnassignquot for the following line of codeStack Overflow
版权声明:本文标题:javascript - how to fix Es-lint "no-return-assign" for the following line of code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744658971a2618127.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论