亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在頁面上獲取后對數組中的項目進行排序和獲?。―iscord V12)

如何在頁面上獲取后對數組中的項目進行排序和獲取(Discord V12)

動漫人物 2023-05-18 10:36:27
我試圖獲得 covid 案例最多的前 5 個國家/地區,但是當我嘗試為此調整另一個代碼時,出現錯誤。 fetch("https://pomber.github.io/covid19/timeseries.json") .then(response => response.json()) .then(data => {var countryArr = Object.keys(data).map(i => i);                countryArr.forEach((country) => {                let countryData = data[country];                countryData = countryData[countryData.length - 1];                })                                let countries = getCovidRank(countryArr, message)                countries.map((countryData, index) => {                    countries[index] = `\\?? #${index+1} | **Country**: \`\`${countryData[0]}\`\` | **Confirmed**: \`\`${countryData[0].confirmed}\`\``                    })//The rest of the code will always work, the problem is up there}這是 getCovidRank 文件:getCovidRank: (countryArr, message) => {        let countryList = []        for(var country in countryArr) {           let countryData = countryArr.confirmed            countryList.push([countryArr, (countryData[country])])        }                countryList.sort((countryData1, countryData2) => {            return countryData2[1] - countryData1[1] || countryData2[2] - countryData1[2]        })        return countryList;            }我真的希望你能幫助我,我正在努力學習如何做到這一點。下面的代碼顯示了全局情況(這段代碼很好):            var worldStats = { confirmed: 0, recovered: 0, deaths: 0 };            var countryArr = Object.keys(data).map(i => i);            countryArr.forEach((country) => {            let countryData = data[country];            // pick last object for today data                countryData = countryData[countryData.length - 1];                worldStats.confirmed += countryData.confirmed;                worldStats.recovered += countryData.recovered;                worldStats.deaths += countryData.deaths;            });
查看完整描述

1 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

這是一個非常簡單的演示,按確認狀態顯示前 5 個國家。

編輯剛剛添加了一個工作片段

function init() {

  fetch("https://pomber.github.io/covid19/timeseries.json")

    .then(response => response.json())

    .then(data => {

      var countries = [];

      Object.keys(data).forEach(country => {

        var total = data[country].reduce(

          (acc, val) => (acc += val.confirmed),

          0

        );

        countries.push({

          name: country,

          confirmed: total

        });

      });

      countries.sort((a, b) => a.confirmed > b.confirmed ? -1 : 1);

      document.getElementById('div').innerHTML = countries.slice(0, 5).map(c => c.name + ' ' + c.confirmed).join('<br>');

    })

}


init();

<div id="div"></div>


查看完整回答
反對 回復 2023-05-18
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號