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));
添加回答
舉報