I had a solution where the filtering of item tags worked. Updated so the books could receive multiple tags and added the tag array:
const bookListBefore = [
{ id: 1, tag: 'Fysik' },
{ id: 2, tag: 'Marketing' },
{ id: 3, tag: '' },
];
const bookListNow = [
{ id: 1, tag: ['Fysik', 'Matematik'] },
{ id: 2, tag: ['Analytics', 'Marketing', 'Data'] },
{ id: 3, tag: [''] },
];
Now I am struggling on finding a solution to filter those items that have a specific tag. Before with the single tags I could perform a filter and could display items with specific tags with this solution:
const filteredList = bookList
.filter(item => (item.title.toLowerCase().includes(searchTextHome)
|| item.author.toLowerCase().includes(searchTextHome))
&& (selectedTags.length > 0 ? selectedTags.includes(item.tag) : true));
<section className={stylesSCSS.bookContainer}>
{filteredList.length === 0 ? emptyPlaceholder()
: filteredList.map(item => (
<BookItem
cover={item.cover}
item={item}
userTagData={userTagData}
onTagSelected={onTagSelected}
/>
))
}
</section>
The first part of the "search" filtering the bookList is about an input search field but this second part (selectedTags.length > 0 ? selectedTags.includes(item.tag) : true) Is where I am not able to filter the tag array, and have no ideas on how to use maybe spread operator or array functions to filter the tag arrays. Any ideas?
selectedTagslooks like ? is it an array or string ?