I have a variable called data
and have it defined to a list like this:
[
{
name: "Richard",
searchable_names:["rich", "dick", "richard", "richie"]
},
{
name: "Anthony",
searchable_names:["tony", "anthony"]
},
]
Using onKeyUp in the search bar, I am trying to filter the results into a new array and display those like this but I realize this is an O(N^2)
nested loop and not the most efficient way of doing it. What is a better way for me to resolve this inefficiency:
data.forEach(name => {
name.searchable_names.forEach(x => {
if (x.toLowerCase().includes(searchBar.text.toLowerCase())) {
arr.push(name);
}
})
})