编码 WorldExplorer 以探索世界人口数据
1. 数组的 map 函数
数组类型的 map
函数非常实用,它能让我们编写更具函数式风格的代码。无需手动遍历数组来构建新数组,只需将每个元素映射到新类型,并保留结果数组的引用。你可以在 这里 了解更多关于 Array.map
的信息。
2. 实现 getCountry 方法
getCountry
方法用于根据国家代码获取国家信息,其实现步骤如下:
async getCountry(countryCode: string): Promise<Country> {
if(!countryCode || '' === countryCode.trim()) {
throw new Error("The country code must be provided!");
}
const response: Response = await fetch(`${this.countriesApiBaseUrl}/${countryCode}?${WorldBankApiV2Params.FORMAT}=${WorldBankApiV2Formats.JSON}`);