I have an array of objects that looks like this:
const pets = [
{name: "Dog", tags: "ground, pet, active"},
{name: "Cat", tags: "ground, pet, relaxed"},
{name: "Fish", tags: "water, pet, exotic"},
]
I want to filter out the array based on the tags
key from a given keyword:
const search = "ground"
const result = pets.filter((pet) => pet.tags.includes(search))
It outputs this:
[
{ name: 'Dog', tags: 'ground, pet, active' },
{ name: 'Cat', tags: 'ground, pet, relaxed' }
]
What if I want to filter out the pets
array with multiple keywords on the tags
like this:
const search = ["ground", "active"]
In theory, only the { name: 'Dog', tags: 'ground, pet, active' }
should be found given the two keywords.
Any ideas how can I achieve something like this?