我正在使用类:
export class Countries {
[key: string]: ICountry;
}
其中ICountry
是:
export interface ICountry {
name: {
wiki?: string,
common: string,
official: string,
native: IStringToOfficialCommon
},
demonym: string,
capital: string,
iso_3166_1_alpha2: string,
iso_3166_1_alpha3: string,
iso_3166_1_numeric: string,
currency: any[] | {
[key: string]: {
iso_4217_code: string,
iso_4217_numeric: number | string,
iso_4217_name: string,
iso_4217_minor_unit: number
}
},
tld: string[],
alt_spellings: string[],
languages: IStringToString | any[],
translations: IStringToOfficialCommon | any[],
geo: {
continent: IStringToString,
postal_code: boolean,
latitude: string,
latitude_dec: string,
longitude: string,
longitude_dec: string,
max_latitude: string,
max_longitude: string,
min_latitude: string,
min_longitude: string,
area: number,
region: string,
subregion: string,
world_region: string,
region_code: string,
subregion_code: string,
landlocked: boolean,
borders: string[],
independent: string,
},
dialling: {
calling_code: string[],
national_prefix: string,
national_number_lengths: number[],
national_destination_code_lengths: number[],
international_prefix: string | number
},
extra: {
geonameid: number,
edgar: string | number,
itu: string | number,
marc: string | number,
wmo: string | number,
ds: string | number,
fifa: string | number,
fips: string | number,
gaul: number,
ioc: string | number,
cowc: string | number,
cown: number,
fao: number,
imf: number,
ar5: string | number,
address_format: string,
eu_member: boolean,
vat_rates: {
standard: number,
reduced: number[],
super_reduced: number,
parking: number
}
},
population: {
count: number,
worldPercentage: number
},
wikiLink: string,
isOutlinesImgExist?: boolean,
isCoatOfArmsImgExist?: boolean,
}
示例数据:
export const CountriesData: Countries = {
"AW": {
"name": {
"common": "Aruba",
"official": "Aruba",
"native": {
"nld": {
"official": "Aruba",
"common": "Aruba"
},
"pap": {
"official": "Aruba",
"common": "Aruba"
}
}
},
"demonym": "Aruban",
"capital": "Oranjestad",
"iso_3166_1_alpha2": "AW",
"iso_3166_1_alpha3": "ABW",
"iso_3166_1_numeric": "533",
"currency": {
"AWG": {
"iso_4217_code": "AWG",
"iso_4217_numeric": 533,
"iso_4217_name": "Aruban Florin",
"iso_4217_minor_unit": 2
}
},
"tld": [
".aw"
],
"alt_spellings": [
"AW"
],
"languages": {
"nld": "Dutch",
"pap": "Papiamento"
},
"translations": {
"deu": {
"official": "Aruba",
"common": "Aruba"
},
"fra": {
"official": "Aruba",
"common": "Aruba"
},
"hrv": {
"official": "Aruba",
"common": "Aruba"
},
"ita": {
"official": "Aruba",
"common": "Aruba"
},
"jpn": {
"official": "\u30a2\u30eb\u30d0",
"common": "\u30a2\u30eb\u30d0"
},
"nld": {
"official": "Aruba",
"common": "Aruba"
},
"por": {
"official": "Aruba",
"common": "Aruba"
},
"rus": {
"official": "\u0410\u0440\u0443\u0431\u0430",
"common": "\u0410\u0440\u0443\u0431\u0430"
},
"spa": {
"official": "Aruba",
"common": "Aruba"
},
"fin": {
"official": "Aruba",
"common": "Aruba"
}
},
"geo": {
"continent": {
"NA": "North America"
},
"postal_code": false,
"latitude": "12 30 N",
"latitude_dec": "12.506523132324219",
"longitude": "69 58 W",
"longitude_dec": "-69.96931457519531",
"max_latitude": "12.616667",
"max_longitude": "-69.85",
"min_latitude": "12.416667",
"min_longitude": "-70.066667",
"area": 180,
"region": "Americas",
"subregion": "Caribbean",
"world_region": "AMER",
"region_code": "019",
"subregion_code": "029",
"landlocked": false,
"borders": [
],
"independent": "Part of NL"
},
"dialling": {
"calling_code": [
"297"
],
"national_prefix": null,
"national_number_lengths": [
7
],
"national_destination_code_lengths": [
2
],
"international_prefix": "00"
},
"extra": {
"geonameid": 3577279,
"edgar": "1C",
"itu": "ABW",
"marc": "aw",
"wmo": "NU",
"ds": 0,
"fifa": "ARU",
"fips": "AA",
"gaul": 14,
"ioc": "ARU",
"cowc": 0,
"cown": 0,
"fao": 0,
"imf": 314,
"ar5": "LAM",
"address_format": null,
"eu_member": null,
"vat_rates": null
},
"population": {
"count": 110108,
"worldPercentage": 0.0015
},
"wikiLink": "\/wiki\/Aruba"
},
"AF": {
"name": {
"common": "Afghanistan",
"official": "Islamic Republic of Afghanistan",
"native": {
"pus": {
"official": "\u062f \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646 \u0627\u0633\u0644\u0627\u0645\u064a \u062c\u0645\u0647\u0648\u0631\u06cc\u062a",
"common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646"
},
"prs": {
"official": "\u062c\u0645\u0647\u0648\u0631\u06cc \u0627\u0633\u0644\u0627\u0645\u06cc \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646",
"common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646"
},
"tuk": {
"official": "Owganystan Yslam Respublikasy",
"common": "Owganystan"
}
}
},
"demonym": "Afghan",
"capital": "Kabul",
"iso_3166_1_alpha2": "AF",
...
所以,我需要将countriesData
转换为Array<ICustomCountry>
,其中ICustomCountry
将是:
interface ICustomCountry {
name: string;
code: string;
}
因此,ICustomCountry.name
映射自ICountry.name.common
,ICustomCountry.code
映射自ICountry.iso_3166_1_alpha2
。
有什么想法吗?
答案 0 :(得分:0)
使用Object.keys
和Array.prototype.map()
let out = Object.keys(countriesData).map(key=>{
let country= countriesData[key]
return {name:country.name.common,code:country.iso_3166_1_alpha2}
});
答案 1 :(得分:0)
如果我理解你的话:
let countries: ICustomCountry[] = Object.keys(CountriesData).map(name => {
const country = CountriesData[name];
return {
name: country.name.common,
code: country.iso_3166_1_alpha2
}
});
另一件事是Countries
NOT 应该是一个类,你没有实例化它(不使用new
),你只为它分配一个对象。
它应该是一个界面。