如何在JavaScript中使用过滤器方法?

发布于 2025-02-03 06:59:31 字数 501 浏览 2 评论 0原文

我正在使用过滤器方法从选项列表中获取所选项目。但这给了我以下错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅的被狗咬 2025-02-10 06:59:31

作为过滤器回调提供的箭头函数应始终通过此弹性规则返回一些值。但是这里...

item => {
  if(item.value !=="Add type") {
    return selectedSet.has(item.value); 
  }
);

...如果条件是虚假的,则没有返回语句。因此错误。您可以以多种不同的方式解决此问题;最直接的一个只是在功能末尾添加explicit 返回false;。另一种方法是结合两个条件:

item => {
  return item.value !=="Add type" && selectedSet.has(item.value) 
}

The arrow function supplied as filter callback should always return some value by this linting rule. But here...

item => {
  if(item.value !=="Add type") {
    return selectedSet.has(item.value); 
  }
);

... there are no return statements if if condition is falsy; hence the error. You can fix this in many different ways; the most straightforward one is just adding explicit return false; at the end of the function. Another approach is combining two conditions:

item => {
  return item.value !=="Add type" && selectedSet.has(item.value) 
}
倾其所爱 2025-02-10 06:59:31

只需返回一个值,如果语句为false(在}封闭括号之后),则应该消失。

Just return a value if statement is false (after if } enclosing bracket) and error should be gone.

信仰 2025-02-10 06:59:31

您必须这样改变:

const getSelectValue = () => {
        const selectedList = get(state, `${name}`, []);
        if (isEmpty(selectedList) || isEmpty(options)) {
            return [];
        }
        const selectedSet = new Set(selectedList);
        return options.filter(item => {
           
                return item.value !=="Add type" && selectedSet.has(item.value); 
        }  );
    };

you have to change like this :

const getSelectValue = () => {
        const selectedList = get(state, `${name}`, []);
        if (isEmpty(selectedList) || isEmpty(options)) {
            return [];
        }
        const selectedSet = new Set(selectedList);
        return options.filter(item => {
           
                return item.value !=="Add type" && selectedSet.has(item.value); 
        }  );
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文