如何在JavaScript中基于2位数的国家名称获得3位数的货币字母和国家代码?

des4xlb0  于 2023-03-16  发布在  Java
关注(0)|答案(2)|浏览(192)

是否有任何软件包或方法可以获得基于2位数国家名称的3位数字母的货币代码和国家代码?
例如:-如果我给予两位数的国家名称“我们”,那么我应该得到货币代码为“美元”和国家代码“美国”。像明智的印度,如果我给国家名称为“在”,那么我应该得到货币代码为“印度卢比”和国家代码“印度”。
好吧,我没有找到任何软件包,到目前为止,给予货币和代码的基础上2位数的国家名称

wwtsj6pe

wwtsj6pe1#

您可以使用country-data npm软件包
用途

const countryData = require("country-data");

const getCountryCodeAndCurrency = (countryCode) => {
    const country = countryData.countries[countryCode];

    return country
        ? { code: country.alpha3, currency: country.currencies[0] }
        : null;
};

console.log(getCountryCodeAndCurrency('IN')); // {"code":"IND","currency":"INR"}
console.log(getCountryCodeAndCurrency('US')); // {"code":"USA","currency":"USD"}
xytpbqjk

xytpbqjk2#

看起来你有很多艰苦的工作要做。我不相信有一个预制的数据库,但你可以很容易地实现它与一个简单的对象。

const currencies = {
  "us": { code: "USD", country: "USA" },
  "in": { code: "INR", country: "IND" },
  // ...
}

currencies["in"] // <-- { code: "INR", country: "IND" }

相关问题