I have a simple array containing Dutch city names:
var cities = ['Amsterdam', 'Den Haag', 'Den Helder', 'Rotterdam', 'Utrecht', 'Groningen', 'Zoetermeer', 'Zwolle', 'Delft'];
My code to search through this array looks like this:
elems.searchField.on('keyup', function(e) {
var index, city, result;
var currentCity = elems.searchField.val().toLowerCase();
for (index = 0; index < cities.length; ++index) {
city = cities[index].toLowerCase();
if (city.indexOf(currentCity) > -1) {
result = city;
console.log(result);
break;
}
}
});
elems.optionsButton.on('click', function(e) {
if (optionButtonState === 0) {
elems.consoleItemSecondAndThird.css('display', 'flex');
optionButtonState = 1;
} else {
elems.consoleItemSecondAndThird.hide();
optionButtonState = 0;
}
});
You can see two cities that start with "Den", these are "Den Haag" and "De Helder". When I type "Den", I need to see them both listed, however, my code only looks up unique values and only shows "Den Haag" when "Den" is inserted for example. How can I make it display all the values that contain the search query?
result.push(city); continue;