SHARE
TWEET

Untitled

a guest Aug 22nd, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 過濾資料
  2. function findMatch (wordToMatch, cityList) {
  3.   let regex = new RegExp(wordToMatch, 'gi')
  4.   return cityList.filter((place) => {
  5.     return place.city.match(regex) || place.state.match(regex)
  6.   })
  7. }
  8.  
  9. // 將數字 format 為千分位一個 ,
  10. function numberWithComma (x) {
  11.   return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')    
  12. }
  13.  
  14. // 顯示資料
  15. function showMatch () {    
  16.   let matchData = findMatch(this.value, cityList)
  17.   let regex = new RegExp(this.value, 'gi')    
  18.   let html = matchData.map(place => {      
  19.   let matchCity = place.city.match(regex)
  20.   let matchState = place.state.match(regex)                  
  21.   let cityName = place.city.replace(regex, `<span class="hl">${this.value ? matchCity : this.value}</span>`)      
  22.   let stateName = place.state.replace(regex, `<span class="hl">${this.value ? matchState: this.value}</span>`)      
  23.     return  `
  24.       <li>
  25.         <span class="name">${cityName}, ${stateName}</span>
  26.         <span class="population">${numberWithComma(place.population)}</span>
  27.       </li>
  28.     `
  29.   }).join('')
  30.  
  31.   document.querySelector('.suggestions').innerHTML = html
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top