I am working on a personal project where I have a dropdown filter that displays cards based on the category. Some cards have multiple category descriptions. I am trying to filter the results based off the select options. I am having trouble selecting any objects that contain the categories in the dropdown. I am easily able to do this if it only has one category, but if it was multiple it will not work.
How do you go about that? Here is some test code to illustrate my intentions.
In the rest code I want to return all objects that have a pet category
const filteredResults = [
{
name: 'George',
height: `6'1`,
weight: '230lbs',
pets: ['dog, cat']
}, {
name: 'Carl',
height: `6'1`,
weight: '230lbs',
pets: ['mouse']
}, {
name: 'Jeff',
height: `6'1`,
weight: '230lbs',
pets: ['penguin, cat']
}, {
name: 'Harry',
height: `6'1`,
weight: '230lbs',
pets: ['horse, cat']
}, {
name: 'Larry',
height: `6'1`,
weight: '230lbs',
pets: ['goat, horse']
}, {
name: 'Tom',
height: `6'1`,
weight: '230lbs',
pets: ['rat, pig']
}, {
name: 'Stacy',
height: `6'1`,
weight: '230lbs',
pets: ['fish, cat']
}, {
name: 'John',
height: `6'1`,
weight: '230lbs',
pets: ['dog, monkey']
}, {
name: 'Tim',
height: `6'1`,
weight: '230lbs',
pets: ['dog, tiger']
},
]
let updatedList = filteredResults.filter(item => {
return item.name === 'Tim'
})
let updatedList2 = filteredResults.filter(item => {
return item.pets === 'cat'
})
console.log(updatedList);
console.log(updatedList2);