I want to be able to add array values to a setState in React JS. Basically I'm doing a search bar for cities. Every time the user types, it will update the setState to include a more filtered search. Basically, if the user wants to search for 'Los Angeles, California', they'll type in 'L' first. So then the program searches for all the cities that start with 'L' and add them to the setState. Then the user proceeds 'Lo', then the program searches for all the cities that start with 'Lo' and updates the setState with only cities that start with 'Lo' and so on and so forth.
For some reason, it always repeats the same city 100 times. So if the person types in 'L' it adds the first city 100 times to setState instead of the first 100 different cities. Below is my code. I'm using it in a for loop.
cities is already a state with every city and state in the US. thisCity and thisState is already declared.
for (var i = 0; i < cities.length; i++){
if(cities[i].city.toUpperCase().startsWith(e.target.value.toUpperCase())){
thisCity = cities[i].city;
thisState = cities[i].state;
setSuggest((suggest) => [...suggest, {id: Math.random() * 1000,
city: thisCity,
state: thisState}])
}
}