【问题标题】:Multiple filter conditions in LodashLodash中的多个过滤条件
【发布时间】:2018-10-04 03:18:03
【问题描述】:

我有一个包含产品信息的对象数组,其中有类别和颜色作为字段。现在,我有另一个数组,其中包含要显示的类别和颜色。

主数组:

products: [{
id: 1,
  name: 'Product 1',
  category: 'Home',
  skill: 'Easy',
  color: 'blue',
  price: 100.00
}, {
    id: 2,
  name: 'Product 2',
  category: 'Home',
  skill: 'Intermediate',
  color: 'red',
  price: 120.00
}, {
    id: 3,
  name: 'Product 3',
  category: 'Office',
  skill: 'Intermediate',
  color: 'green',
  price: 190.00
}, {
    id: 4,
  name: 'Product 4',
  category: 'Office',
  skill: 'Advanced',
  color: 'blue',
  price: 260.00
}, {
    id: 5,
  name: 'Product 5',
  category: 'Warehouse',
  skill: 'Advanced',
  color: 'white',
  price: 321.00
}, {
    id: 6,
  name: 'Product 6',
  category: 'Farm',
  skill: 'Intermediate',
  color: 'red',
  price: 120.00
}, {
    id: 7,
  name: 'Product 7',
  category: 'Space',
  skill: 'Advanced',
  color: 'green',
  price: 150.00
}, {
    id: 8,
  name: 'Product 8',
  category: 'Bathroom',
  skill: 'Easy',
  color: 'black',
  price: 9.00
}],

我的另一个包含过滤器选项的数组:

selectedFilters: {
 categories : ["home", "bathroom"] ,
 colors : ["blue", "red"] 
}

现在我希望我的输出为:

[{
id: 1,
  name: 'Product 1',
  category: 'Home',
  skill: 'Easy',
  color: 'blue',
  price: 100.00
}, {
    id: 2,
  name: 'Product 2',
  category: 'Home',
  skill: 'Intermediate',
  color: 'red',
  price: 120.00
}]

我尝试了普通的数组过滤方法:

this.filteredProducts = this.products.filter(prod => {
  // [prod.category, prod.color].some(val => this.selectedFilters.includes(val))
  console.log(this.selectedFilters);
  for (let i = 0; i < this.selectedFilters.length; i++) {
    // console.log('Selected Category : ', this.selectedFilters[i]);
    // console.log('Product Category : ', prod.category);
    if (prod.category == this.selectedFilters[i]) {
      console.log('Called category');
      return true;
    } else if (prod.color == this.selectedFilters[i]) {
      console.log('Called color');
      return true;
    } else {
      console.log('Called else');
      continue;
    }
  }

});

如何使用数组过滤函数或 Lodash 函数实现这一点?

【问题讨论】:

    标签: javascript arrays object filter lodash


    【解决方案1】:

    不需要库,使用标准的.filter方法过滤数组,查看每个对象的颜色和类别是否包含在 selectedFilters 对象中:

    const products = [{
      id: 1,
      name: 'Product 1',
      category: 'Home',
      skill: 'Easy',
      color: 'blue',
      price: 100.00
    }, {
      id: 2,
      name: 'Product 2',
      category: 'Home',
      skill: 'Intermediate',
      color: 'red',
      price: 120.00
    }, {
      id: 3,
      name: 'Product 3',
      category: 'Office',
      skill: 'Intermediate',
      color: 'green',
      price: 190.00
    }, {
      id: 4,
      name: 'Product 4',
      category: 'Office',
      skill: 'Advanced',
      color: 'blue',
      price: 260.00
    }, {
      id: 5,
      name: 'Product 5',
      category: 'Warehouse',
      skill: 'Advanced',
      color: 'white',
      price: 321.00
    }, {
      id: 6,
      name: 'Product 6',
      category: 'Farm',
      skill: 'Intermediate',
      color: 'red',
      price: 120.00
    }, {
      id: 7,
      name: 'Product 7',
      category: 'Space',
      skill: 'Advanced',
      color: 'green',
      price: 150.00
    }, {
      id: 8,
      name: 'Product 8',
      category: 'Bathroom',
      skill: 'Easy',
      color: 'black',
      price: 9.00
    }];
    const selectedFilters = {
     categories : ["home", "bathroom"] ,
     colors : ["blue", "red"] 
    };
    
    const { categories, colors } = selectedFilters;
    const filteredProducts = products.filter(({ category, color }) => (
      categories.includes(category.toLowerCase()) && colors.includes(color.toLowerCase())
    ));
    console.log(filteredProducts);

    【讨论】:

    • 感谢您的回答。但这不能正常工作。我在 VueJS 中使用这段代码。你能帮我解决这个问题吗?这是fiddle
    • 您的颜色选择已损坏;它将选择所有这些或不选择任何一个。你的selectedFilters 对象也坏了——它被分配了false,所以当然{ categories, colors } 不能从中解构。您的新问题与您的旧问题无关。
    • 这仅在 selectedFilters 类别和颜色中都存在值时才有效。当 selectedFilters.colors 为空或 selectedFilters.categories 为空时,它会中断。当其中任何一个为空时,它应该返回通过任何一个 selectedFilter 值过滤的数据。
    • 我修补的是:this.filteredProducts = this.products.filter(({ category, color }) =&gt; { if(categories.length &gt; 0 &amp;&amp; colors.length &gt; 0){ return categories.includes(category) &amp;&amp; colors.includes(color) } else if(categories.length &gt; 0 &amp;&amp; colors.length == 0){ return categories.includes(category) } else if(categories.length == 0 &amp;&amp; colors.length &gt; 0){ return colors.includes(category) } else { return categories.includes(category) || colors.includes(color) } });
    猜你喜欢
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多