Skip to content

Commit 7be7dfa

Browse files
committed
Create the regex outside the filter and map callbacks
1 parent 38a754c commit 7be7dfa

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

06 - Type Ahead/index-FINISHED.html

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
.then(data => cities.push(...data));
2424

2525
function findMatches(wordToMatch, cities) {
26-
return cities.filter(place => {
27-
// here we need to figure out if the city or state matches what was searched
28-
const regex = new RegExp(wordToMatch, 'gi');
29-
return place.city.match(regex) || place.state.match(regex)
30-
});
26+
const regex = new RegExp(wordToMatch, 'gi');
27+
28+
// here we need to figure out if the city or state matches what was searched
29+
return cities.filter(place => place.city.match(regex) || place.state.match(regex));
3130
}
3231

3332
function numberWithCommas(x) {
@@ -36,8 +35,8 @@
3635

3736
function displayMatches() {
3837
const matchArray = findMatches(this.value, cities);
38+
const regex = new RegExp(this.value, 'gi');
3939
const html = matchArray.map(place => {
40-
const regex = new RegExp(this.value, 'gi');
4140
const cityName = place.city.replace(regex, `<span class="hl">${this.value}</span>`);
4241
const stateName = place.state.replace(regex, `<span class="hl">${this.value}</span>`);
4342
return `

0 commit comments

Comments
 (0)