首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用LODASH和角13基于状态JSON隔离城市

问如何使用LODASH和角13基于状态JSON隔离城市
EN

Stack Overflow用户
提问于 2022-09-28 14:28:52
回答 1查看 46关注 0票数 0

这有点挑战性,但我马上就到了。

这是我简略的JSON对象,世界上所有的国家和城市。

唯一的例外是美国和加拿大,美国有各州,我得到了,加拿大有我要得到的省份。

JSON包含国家->城市

代码语言:javascript
复制
{
"data": {
    "Cape Verde": ["Praia"],
    "Cayman Islands": [
      "Coral Gables",
      "George Town",
      "West Bay",
      "Savannah",
      "Spot Bay",
      "Bodden Town",
      "Newlands"
    ],
    "Central African Republic": ["Bangui"],
    "Chad": ["Massaguet", "Abéché", "N'Djamena", "Moundou"],
        "United States": [
      "Mukilteo",
      "Fairfield",
      "Chicago",
      "Hernando",
      "Irving",
      "Baltimore",
      "Kingston",
      "Burlington",
      "Harrison",
      "Newton Center",
      "Littleton"]
   }
}

我用前面的类型来填充这些国家,然后在美国,我得到了美国,但是要让城市成为任何一个特定的州都是一个小小的挑战。

这是城市/州的JSON https://medium.com/hackernoon/json-of-5-950-usa-cities-and-their-states-29352c40ad21

这是我目前正在做的工作的代码。

代码语言:javascript
复制
  /**
   * @name getAllCountries
   * @description gets all countries
   * @returns Object
   */
  getAllCountries(): any {
    console.log('FILTERED ALL COUNTRIES: ', this.allCountries.data);
    return this.allCountries.data;
  }

  getStatesFromCountry(country: string): any {

    if (country === 'United States') {
      this.filteredstates = this.states.states;
    } else {
      this.filteredcities = this.getCitiesFromCountry(country);
    }

    console.log('FILTERED STATES: ', this.filteredstates);
    return this.filteredstates;
  }

这是在来自媒体的链接中使用新的JSON对象的挑战

代码语言:javascript
复制
  getCitiesFromState(state: string): any {
    const self = this;
    let cs = [];
    cs.push(this.allStatesAndCities);
    let cities: number[] = [];

    let ret = _.find(cs, function(value, key) {
      if (value === state) {
        cities.push(key);
      }
    })
    this.filteredcities = cities;
    // for (let i = 0; i < cs.length; i++) {
    //   if (cs[i] === state) {
    //     this.filteredcities = cs[i];
    //   }
    // }
    console.log('FILTERED STATES: ', this.filteredcities);
    return this.filteredcities;
  }

这是我的DEV控制台的图片。

​

这是美国的哪个地区-->城市--这是形象中的“价值”。

代码语言:javascript
复制
{
  "New York": [
    "New York",
    "Buffalo",
    "Rochester",
    "Yonkers",
    "Syracuse",
    "Albany",
    "New Rochelle",
    "Mount Vernon",
    "Schenectady",
    "Utica",
    "White Plains",
    "Hempstead",
    "Troy",
    "Niagara Falls",
    "Binghamton",
    "Freeport",
    "Valley Stream"
  ],
  "California": [
    "Los Angeles",
    "San Diego",
    "San Jose",
    "San Francisco",
    "Fresno",
    "Sacramento",
...

下面是上面https://gist.githubusercontent.com/ahmu83/38865147cf3727d221941a2ef8c22a77/raw/c647f74643c0b3f8407c28ddbb599e9f594365ca/US_States_and_Cities.json的JSON

任何帮助都是很好的..。我本质上想要过滤城市,当我把一个州发送到这个函数中,他们排序为and。

EN

回答 1

Stack Overflow用户

发布于 2022-09-28 20:47:04

以下是解决办法:

代码语言:javascript
复制
  getCitiesFromState(state: string): any {
    const self = this;
    let cs = this.allStatesAndCities[0];

    let cities = _.get(cs, state);  <<<<---- _.get worked

  console.log('FILTERED CITIES: ', this.filteredcities);
    return this.filteredcities;
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73883251

复制
相关文章

相似问题

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