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

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

通過JS從站點獲取數據并保存在字典中

通過JS從站點獲取數據并保存在字典中

胡子哥哥 2023-07-14 10:12:39
我想通過 Javascript 從https://api.memegen.link/templates獲取一些數據。有人可以指出我正確的方向或給我一些示例代碼嗎?我想獲取所有的nameandblank并將其存儲在字典中。
查看完整描述

1 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

首先,我們得到一個隨機數(第一行)。然后我們使用fetch()API 從網站獲取 JSON 數據。然后我們將數據轉換為 json,然后從數組中選擇一個隨機對象,然后將 和 記錄name到blank控制臺。這是獲得一件物品的方法。


let random = Math.floor(Math.random() * 10);

fetch("https://api.memegen.link/templates")

    .then(data => data.json())

    .then(data => {

         console.log(data[random].name);

         console.log(data[random].blank);

    })

    .catch(e => console.error(e));

要將所有數據放入字典中,您需要這樣做


fetch("https://api.memegen.link/templates")

    .then(data => data.json())

    .then(data => {

    // The line below is a declaration of a array

            let items = [];

    // Getting every object from the array and pushing the names and images to OUR array

                data.forEach(item => {

                items.push({ name: item.name, blank: item.blank });

        });

    // Logging the array

        console.log(items);

    })

    .catch(e => console.error(e));


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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