I have a collection of items set up in the following format.
items = [
{ id: 1,
status : "Active"
// Other fields
tags : [{val: 'IGM', color: 'light-success' },
{val: 'Gated Out', color: 'light-primary' },
]
},
// ...
]
Now I want to filter these items based on their tags. The input for filtering is an array generated from a multi-select input.
For example:
[{value: 'Gated Out', label: 'GATED OUT'}, .. ]
I am able to filter data based on other fields which are strings, but struggling with filtering based on arrays like tags.
How can I adjust my current code to handle array-based filtering as well?
This is the approach I'm currently following;
const handleTagsFilter = (value) => {
let updatedItems = []
const filterData = () => {
if (
status.length ||
custom_tags.length
) {
return filteredItems
} else {
return items
}
}
setCustomTags(value)
if (value.length) {
updatedItems = filterData().filter((item) => {
const startsWith = item.status.toLowerCase().startsWith(value.toLowerCase())
const includes = item.status.toLowerCase().includes(value.toLowerCase())
if (startsWith) {
return startsWith
} else if (!startsWith && includes) {
return includes
} else return null
})
setFilteredItems([...updatedItems])
setCustomTags(value)
}
}
This function effectively filters strings based on conditions like matching the status field to "Active". However, I need assistance in adapting it to successfully filter arrays like tags.