Arrays 筛选出数组中指定元素为空字符串的项

Arrays 筛选出数组中指定元素为空字符串的项,arrays,reactjs,filtering,Arrays,Reactjs,Filtering,我对JS非常陌生,我尝试创建一个新数组,过滤掉现有数组中具有空值的元素。在下面的示例代码中,我希望创建一个新的数组来过滤掉第三项,因为url是空字符串(我只希望它根据url是否为空字符串进行过滤)。我应该补充一点,const是作为reducer的一部分导出的,在它实际使用的文件中,我们调用了{props.categories}。非常感谢您的帮助 const categories = () => { return [ { id: 0, title: "Go

我对JS非常陌生,我尝试创建一个新数组,过滤掉现有数组中具有空值的元素。在下面的示例代码中,我希望创建一个新的数组来过滤掉第三项,因为url是空字符串(我只希望它根据url是否为空字符串进行过滤)。我应该补充一点,const是作为reducer的一部分导出的,在它实际使用的文件中,我们调用了{props.categories}。非常感谢您的帮助

const categories = () => {
  return [
    {
      id: 0,
      title: "Google",
      url: "www.google.com",
    },
    {
      id: 1,
      title: "Firefox",
      url: "www.firefox.com",
    },
    {
      id: 2,
      title: "Placeholder",
      url: "",
    },
];
};    

数组有一个filter函数,如果当前元素应该被过滤到结果数组中,该函数将返回true/false

常量类别=[
{
id:0,
标题:“谷歌”,
网址:“www.google.com”,
},
{
id:1,
标题:“Firefox”,
网址:“www.firefox.com”,
},
{
id:2,
标题:“占位符”,
url:“”,
},
];
const filterBlankUrl=arr=>arr.filter(({url})=>!!url);
常量filteredCats=filterBlankUrl(类别);

控制台日志(filteredCats)非常感谢大家的帮助!我确实最终找到了答案;在我实际引用数组的文件中,我需要将代码更新为:

options={props.categories.filter(categories => categories.url !== "")}

看来这样可以解决问题了?