这有点挑战性,但我马上就到了。
这是我简略的JSON对象,世界上所有的国家和城市。
唯一的例外是美国和加拿大,美国有各州,我得到了,加拿大有我要得到的省份。
JSON包含国家->城市
{
"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
这是我目前正在做的工作的代码。
/**
* @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对象的挑战
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控制台的图片。

这是美国的哪个地区-->城市--这是形象中的“价值”。
{
"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",
...任何帮助都是很好的..。我本质上想要过滤城市,当我把一个州发送到这个函数中,他们排序为and。
发布于 2022-09-28 20:47:04
以下是解决办法:
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;
}https://stackoverflow.com/questions/73883251
复制相似问题