Unfortunately, JS is small when it comes to internationalized case-insensitive string comparisons. If you just stick to ASCII, the solution is pretty simple (using filter , but not where ):
function getMatches(query, arr) { var lowerQuery = query.toLowerCase(); return _.filter(arr, function(term) { return term.toLowerCase() == lowerQuery; }); }
Or if you want to pre-comprehend everything because you expect to make many requests in one JS session:
var index = _.groupBy(arr, function(term) { return term.toLowerCase(); }); // Post-process index to reduce memory footprint by turning redundant values to nulls // (which take up less memory than arrays with a string in them). index = _.object(_.map(index, function(terms, key) { return [key, (terms.length == 1 && terms[0].toLowerCase() == terms[0] ? null : terms)]; })); function getMatches(query) { var lowerQuery = query.toLowerCase(); return (lowerQuery in index ? index[lowerQuery] || lowerQuery : []); }
This second method has the advantage of only calculating toLowercase() minimum number of times and the minimum data storage due to the post-processing step.
Here's the JSFiddle for
source share