I'm looping through each value in an array and, for each value, I'm looping through a jQuery list of values to find an exact match. When a match is found I reveal its li element. Everything in the array will always be present in the list. This all works OK. However in real life there are 7,000+ items in both the array and the jQuery list and my approach already runs slow because of repeated looping. I've been trying to find a way to speed things up, perhaps with maps, but with my limited knowledge I'm not able to get my head around it. Can anyone help? https://jsfiddle.net/bt9xncea/
<ul>
<li>Paris</li>
<li>London</li>
<li>Puntarenas</li>
<li>Lisbon</li>
<li>Balochistan</li>
<li>Bonn</li>
<li>Helsinki</li>
<li>Moscow</li>
<li>San Jose</li>
<li>Paris</li>
<li>Madrid</li>
<li>Rome</li>
</ul>
var CityList = ["San Jose", "Alajuela", "Caldera", "Puntarenas", "Sisak-Moslavina", "Dalmatia", "", "Thatta", "Sindh", "Punjab", "Islamabad", "Balochistan", "Karachi"];
$("ul li").hide()
var elementText = "";
$.each(CityList, function(index, City) {
$('ul li').each(function(dropindex) {
elementText = $("ul li:eq(" + dropindex + ")").text()
if (elementText === City) {
$("ul li:eq(" + dropindex + ")").show();
return false;
}
});
});