如何在JavaScript中使用过滤器方法?
我正在使用过滤器方法从选项列表中获取所选项目。但这给了我以下错误:
array.prototype.filter()期望在箭头函数末尾返回一个值
我的函数,我的函数如下:
const getSelectValue = () => {
const selectedList = get(state, `${name}`, []);
if (isEmpty(selectedList) || isEmpty(options)) {
return [];
}
const selectedSet = new Set(selectedList);
return options.filter((item) => {
if (item.value !== "Add type") return selectedSet.has(item.value);
});
};
有人可以在这里发现丢失的东西吗?
I am using filter method to get a selected item from an option list. But it gives me the following error:
Array.prototype.filter() expects a value to be returned at the end of arrow function.eslintarray-callback-return
My function is the following:
const getSelectValue = () => {
const selectedList = get(state, `${name}`, []);
if (isEmpty(selectedList) || isEmpty(options)) {
return [];
}
const selectedSet = new Set(selectedList);
return options.filter((item) => {
if (item.value !== "Add type") return selectedSet.has(item.value);
});
};
Anyone who can spot the missing thing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为过滤器回调提供的箭头函数应始终通过此弹性规则返回一些值。但是这里...
...如果条件是虚假的,则没有
返回
语句。因此错误。您可以以多种不同的方式解决此问题;最直接的一个只是在功能末尾添加explicit返回false;
。另一种方法是结合两个条件:The arrow function supplied as filter callback should always return some value by this linting rule. But here...
... there are no
return
statements ifif
condition is falsy; hence the error. You can fix this in many different ways; the most straightforward one is just adding explicitreturn false;
at the end of the function. Another approach is combining two conditions:只需返回一个值,如果语句为false(在}封闭括号之后),则应该消失。
Just return a value if statement is false (after if } enclosing bracket) and error should be gone.
您必须这样改变:
you have to change like this :