按多个类别过滤数组

时间:2020-10-01 20:08:37

标签: javascript

我有多个场所,每个场所可以有多个类别的折扣,用户可以选择按其折扣的类别过滤场所(用户也可以选择多个类别!),我如何只过滤那些谁的场所有所选类别的折扣。


let selectedCategories = [1,3];

let venues =
[
    { name:'Venue 1', discountsOfCategoryOneCount:2, discountsOfCategoryTwoCount:4 },
    { name:'Venue 2', discountsOfCategoryOneCount:1, discountsOfCategoryTwoCount:8 },
    { name:'Venue 3', discountsOfCategoryOneCount:4, discountsOfCategoryTwoCount:3 }
];

let filteredResults = venues.filter((item) => /*HOW TO DO THIS*/);

1 个答案:

答案 0 :(得分:1)

let selectedCategories = [1, 2];

let venues = [
  { name: 'Venue 1', discountsOfCategoryOneCount: 2, discountsOfCategoryTwoCount: 4 },
  { name: 'Venue 2', discountsOfCategoryOneCount: 1, discountsOfCategoryTwoCount: 8 },
  { name: 'Venue 3', discountsOfCategoryOneCount: 4, discountsOfCategoryTwoCount: 3 },
];

let filteredResults = venues.filter((item) => selectedCategories.indexOf(item.discountsOfCategoryOneCount) !== -1);

console.log(filteredResults);