Repository navigation
Expand file tree
/
Copy pathcode.js
More file actions
52 lines (37 loc) · 1.4 KB
/
Copy pathcode.js
File metadata and controls
52 lines (37 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const api =
`https://raw.githubusercontent.com/kravchenkoeag/JS-Dynamic-Search/main/subway.json`;
const stations = [];
fetch(api)
.then(res => res.json())
.then(data => {
console.log('data >>>> ', data);
data.forEach(line => {
stations.push(...line.stations);
})
});
const searchInput = document.querySelector('.search');
const searchOptions = document.querySelector('.options');
function getOptions(word, stations) {
return stations.filter(s => {
// Определить совпадает ли то что мы вбили в input
const regex = new RegExp(word, 'gi')
return s.name.match(regex)
})
}
function displayOptions() {
console.log('this.value >> ', this.value);
const options = getOptions(this.value, stations);
const html = options
.map(station => {
const regex = new RegExp(this.value, 'gi');
const stationName = station.name.replace(regex,
`<span class="hl">${this.value}</span>`
)
return `<li><span>${stationName}</span></li>`
})
.slice(0, 10)
.join('');
searchOptions.innerHTML = this.value ? html : null;
}
searchInput.addEventListener('change', displayOptions);
searchInput.addEventListener('keyup', displayOptions);