JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<input class="query-field" type="text" value="ford"></input><br>
<button class="method-a">Query with method A</button><br>
<button class="method-b">Query with method B</button><br>
<div class="results"></div>
JavaScript
var arr = [
'apple',
'Banana',
'ford',
'Ford',
'duPont'
];
// METHOD A
function getMatchesA(query) {
var lowerQuery = query.toLowerCase();
return _.filter(arr, function(term) {
return term.toLowerCase() == lowerQuery;
});
}
// METHOD B
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 getMatchesB(query) {
var lowerQuery = query.toLowerCase();
return (lowerQuery in index ? index[lowerQuery] || lowerQuery : []);
}
// UI stuff
$('.method-a').click(function() {
$('.results').text('[' + getMatchesA($('.query-field').val()) + ']');
});
$('.method-b').click(function() {
$('.results').text('[' + getMatchesB($('.query-field').val()) + ']');
});