您可以使用三个条件:
const finalFilteredAnimals = filteredAnimals.filter(({ animal_type }) => (
(dog.present || !animal_type.includes('Dog')) &&
(cat.present || !animal_type.includes('Cat')) &&
(fish.present || !animal_type.includes('fish'))
));
注意,如果
dog
,
cat
和
fish
在一个更大的物体里,比如说
animals
,以便可以在
['Dog', 'Cat', 'fish']
取而代之的是:
const finalFilteredAnimals = filteredAnimals.filter(({ animal_type }) => (
['Dog', 'Cat', 'fish'].every(animName => (
animal[animName].present || !animal_type.includes(animName)
))
));