I’m trying to find a fast way to search for partial strings in array elements and put them into a new array if the input value is greater than 2 characters.
Ultimately, this array will be used to create a list as results are found, a local data search.
This works okay but the arrays can be quite large (thousands of entries) and was looking for the best way to approach this that won’t end up a performance problem.
<html>
<body>
<input id="test">
</body>
<script>
let test = ['Fruits: Apples', 'Fruits: Oranges', 'Fruits: Banannas', 'Fruits: Lemons', 'Vegetables: Corn', 'Vegetables: Potatoes']
let found = []
document.querySelector('#test').addEventListener('keyup', e => {
let value = e.currentTarget.value
if (value.length > 2) {
let index = test.findIndex(i => i.includes(value))
test.forEach(v => {
if(v.includes(value) && !found.includes(v))
found.push(v)
})
} else {
found = []
}
console.log(found)
})
</script>
</html>