打字稿:映射键:值结构

  • 本文关键字:结构 映射 typescript
  • 更新时间 :
  • 英文 :


我正在使用这样的类:

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": "u30a2u30ebu30d0",
        "common": "u30a2u30ebu30d0"
      },
      "nld": {
        "official": "Aruba",
        "common": "Aruba"
      },
      "por": {
        "official": "Aruba",
        "common": "Aruba"
      },
      "rus": {
        "official": "u0410u0440u0443u0431u0430",
        "common": "u0410u0440u0443u0431u0430"
      },
      "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 u0627u0641u063au0627u0646u0633u062au0627u0646 u0627u0633u0644u0627u0645u064a u062cu0645u0647u0648u0631u06ccu062a",
          "common": "u0627u0641u063au0627u0646u0633u062au0627u0646"
        },
        "prs": {
          "official": "u062cu0645u0647u0648u0631u06cc u0627u0633u0644u0627u0645u06cc u0627u0641u063au0627u0646u0633u062au0627u0646",
          "common": "u0627u0641u063au0627u0646u0633u062au0627u0646"
        },
        "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映射的。

有什么想法吗?

使用Object.keysArray.prototype.map()

let out = Object.keys(countriesData).map(key=>{
    let country= countriesData[key]
    return {name:country.name.common,code:country.iso_3166_1_alpha2}
});

如果我当时理解你的话:

let countries: ICustomCountry[] = Object.keys(CountriesData).map(name => {
    const country = CountriesData[name];
    return {
        name: country.name.common,
        code: country.iso_3166_1_alpha2
    }
});

另一件事是Countries不应该是一个类,你不实例化它(不使用new(,你只给它分配一个对象。
它应该是一个接口。

最新更新