首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套JSON中的LiveSearch

嵌套JSON中的LiveSearch
EN

Stack Overflow用户
提问于 2021-03-22 12:42:12
回答 1查看 60关注 0票数 0

在所示的示例中,搜索国家的名称。它位于json对象的第一级。

我如何搜索一个水平下降的字符串?作为altSpellings中的一个例子

代码语言:javascript
复制
[{
"name":"Afghanistan", 
"altSpellings":["AF","Afġānistān"],
"region":"Asia",
}]

如果我将过滤器中的行更改为:return place.altSpellings.match(regex)

我得到了一个错误:Uncaught TypeError: place.altSpellings.match is not a function

代码语言:javascript
复制
const countries = [];
fetch('https://restcountries.eu/rest/v2/all')
  .then(response => response.json())
  .then(json => countries.push(...json));

function searchCountries(wordToSearch, countries) {
  return countries.filter(place => {
    const regex = new RegExp(wordToSearch, 'gi');
    return place.name.match(regex)
  })
}

function liveSearch() {
  if (this.value.length) {
    const searchResult = searchCountries(this.value, countries);

    const searched = searchResult.map(item => {
      const regex = new RegExp(this.value, 'gi');
      const name = item.name.replace(regex, `<b>${this.value}</b>`)
      const altSpellings = item.altSpellings

      return `<div>${name}<br>${altSpellings}<br><br></div>`;

    }).join('');

    results.innerHTML = searched;
  } else {
    results.innerHTML = "";
  }
}

const search = document.querySelector('.search');
const results = document.querySelector('.results');
search.addEventListener('keyup', liveSearch);
代码语言:javascript
复制
<div class="wrapper">
  <h1>Ajax Search countries</h1>
  <input type="text" class="search" placeholder="search any country here" />
  <div class="results"></div>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-22 13:18:15

你能这样试试吗?

代码语言:javascript
复制
const countries = [];
fetch('https://restcountries.eu/rest/v2/all')
  .then(response => response.json())
  .then(json => countries.push(...json));

function searchCountries(wordToSearch, countries) {
  return countries.filter(({name, altSpellings}) => {
    let isMatched = false
    const regex = new RegExp(wordToSearch, 'gi');
    const namesArr = [name, ...altSpellings]
    namesArr.forEach(v => {
     if (v.match(regex)) {
       isMatched = true
     }
    })
 
    return isMatched
  })
}

function liveSearch() {
  if (this.value.length) {
    const searchResult = searchCountries(this.value, countries);

    const searched = searchResult.map(item => {
      const regex = new RegExp(this.value, 'gi');
      const name = item.name.replace(regex, `<b>${this.value}</b>`)
      const altSpellings = item.altSpellings.map(v => v.replace(regex,`<b>${v}</b>`))
      return `<div>${name}<br>${altSpellings}<br><br></div>`;

    }).join('');

    results.innerHTML = searched;
  } else {
    results.innerHTML = "";
  }
}

const search = document.querySelector('.search');
const results = document.querySelector('.results');
search.addEventListener('keyup', liveSearch);
代码语言:javascript
复制
<div class="wrapper">
  <h1>Ajax Search countries</h1>
  <input type="text" class="search" placeholder="search any country here" />
  <div class="results"></div>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66746282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档