Created
January 5, 2017 14:58
-
-
Save danieluhl/3fc3932f3ff116dd612073ca66b29ded to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json'; | |
const list = document.querySelector('.suggestions'); | |
const input = document.querySelector('.search'); | |
const populateList = data => { | |
const html = data.reduce((acc, item) => { | |
acc = `${acc} | |
<li> | |
<span class="name">${item.city}, ${item.state}</span> | |
<span class="population">${item.population}</span> | |
</li>`; | |
return acc; | |
}, ''); | |
list.innerHTML = html; | |
} | |
function inputKeydownHandler(e) { | |
const value = this.value; | |
const filteredData = allData.filter(item => | |
item.city.toLowerCase().includes(this.value) || | |
item.state.toLowerCase().includes(this.value) | |
); | |
populateList(filteredData); | |
} | |
input.addEventListener('keyup', inputKeydownHandler); | |
let allData = []; | |
fetch(endpoint).then(res => res.json()).then((data) => { | |
allData = data; | |
return data; | |
}).then(populateList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment