I was trying to search for function. This function checks the nested array, which has multiple strings, then returns if the array has a search word.
The data structure of the array
[
{
name:'aa',
searchWords:['aa','ab','bc'] <- I want to use this array for search
},
{
name:'bb',
searchWords:['bb','bc','de'] <- I want to use this array for search
},
...
]
I tried to use indexOf function.
However, I couldn't display any components with this code below.
But I changed target value from item.searchWords to item.name in the code. it worked.
HTML
<div className="itemWrapper">
{filterItems.map((item, idx) => (
<Item key={idx} {...item} id={idx} list={list} />
))}
</div>
Js
const filterItems = list.filter(
(item) => item.searchWords.indexOf(searchWord) !== -1,
);
My desired result is that search result update in realtime.
For instance with the same data structure above, when the user searches 'a', searchWords 'aa' and 'ab' returns true for displaying the item,
Thank you so much for your help.
ait would match item.name (with say first letter) but not any word ofitem.searchWords)b?