gpt4 book ai didi

javascript - Angular JS。在我只有国家 ID 的情况下在 UI 中显示国旗

转载 作者:行者123 更新时间:2023-11-28 04:06:17 25 4
gpt4 key购买 nike

在我的 JSON 结构中有 countryIds 字段:

[
{countryIds: [1, 35, 16]}
{countryIds: [6, 21]}
]

在 UI 中,我想显示国旗而不是国家 ID。

因此,为此我想我可以创建服务,它将返回图标标志的 url:

getCountryFlagIconUrlById(countryId) {
switch(countryId) {
case 1: return "http://mySite:8080/resources/usa.png"
.....
case 200: return "http://mySite:8080/resources/russia.png"
}

但问题是我需要为 200 多个国家/地区创建 switch-case 运算符。

有没有更优雅的方法呢?

最佳答案

只用一张 map

var countries = { 
1: 'usa',
...,
200: 'russia'
};

使用 getCountryFlagIconUrlById 如下:

function getCountryFlagIconUrlById(id) {
return 'http://localhost:8080/resources/'+countries[id]+'.png';

}

目前的缺点是它不检查 id 是否存在,但这是一个简单的 if 语句。

关于javascript - Angular JS。在我只有国家 ID 的情况下在 UI 中显示国旗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25240907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com