从json抓取特定数据

时间:2020-07-14 03:11:36

标签: javascript discord discord.js

        const URL = ["https://api.covid19api.com/summary"];
        let global = await fetch(URL);
        let data = await global.json();


        const COVID_EMBED = new Discord.RichEmbed()
        .setTitle(`__**Global COVID-19 Status Tracker:**__`)
        .setDescription(`Placeholder_description.`)
        .addField(`Newly Confirmed Cases: `, data.Global.NewConfirmed, true)
        .addField(`Total Confirmed Cases: `, data.Global.TotalConfirmed, true)
        .addField(`New Deaths: `, data.Global.NewDeaths, true)
        .addField(`Total Deaths: `, data.Global.TotalDeaths, true)
        .addField(`Newly Recovered Cases: `, data.Global.NewRecovered, true)
        .addField(`Total Recovered Cases: `, data.Global.TotalRecovered, true)
        .setTimestamp()
        .setColor(0)
 return message.channel.send ({embed: COVID_EMBED });

我正在执行covid命令。

以上是获取“全局”案例等的代码,尽管URL here也具有特定的国家/地区,但我将如何获取特定的国家/地区?因此,要代替“ data.Global.NewRecovered”,我要怎么做以获取“希腊”呢?

2 个答案:

答案 0 :(得分:1)

在提供的json响应中,有一个国家/地区列表。如果您要使用特定列表(例如希腊),则Javascript可以使用find()来搜索列表:

let greeceStatistics = data.Countries.find(countryData => countryData.CountryCode === "GR");

答案 1 :(得分:0)

您可以按国家/地区名称使用filter:

JS代码,名为covidData的JSON数据

function countryData(countryName){
        let res = covidData.Countries.filter(countrie=>countrie.Country===countryName)
    return res
}

//console.log(countryData('Greece'))