-->
当前位置:首页 > articles > 正文内容

Luz1个月前 (10-28)articles263

How do I pick a random object from an array that I fetched from an API?

  • Harper Klotz 问题:
    • // gets data for playable games in browser only and logs to consoleconst options = {    method: 'GET',    headers: {        'X-RapidAPI-Key': 'INSERT-KEY-HERE',        'X-RapidAPI-Host': 'free-to-play-games-database.p.rapidapi.com'    }};fetch('https://free-to-play-games-database.p.rapidapi.com/api/games?platform=browser', options)    .then(response => response.json())    .then(response => {            console.log(response)        })    .catch(err => console.error(err));
    • //pulls random gamefunction gameApi(selection){    console.log(selection);    var apiURL=('free-to-play-games-database.p.rapidapi.com'+ selection)    console.log(apiURL);    fetch(apiURL).then(function(response) {        return response.json();    }) .then(function(data) {        var random = Math.floor(Math.random()*data.response.length);        var randomGame = (data.response[random].strGame);        console.log(randomGame);        var gameUrl = ('https://free-to-play-games-database.p.rapidapi.com/api/games?    platform=browser'+randomGame);        console.log(gameUrl);        fetch(gameUrl).then(function(response) {            return response.json();        }).then(function (data){            console.log(data);        })    });}
  • 回答:
返回列表

上一篇:

下一篇:

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。