javascript - 何时比较 'for' 循环中的多个数组

标签 javascript google-apps-script steam

我对 Javascript 和 Google Apps 脚本还很陌生,我正在尝试创建一个脚本,该脚本可以获取您 friend 的 Steam ID,循环遍历他们拥有的游戏,将它们列出到电子表格中,并显示某人是否拥有游戏与否。

我已经实现了第一部分,循环遍历每个 ID 的所有拥有的游戏,并将它们添加到数组中(如果数组中尚不存在),可以完美地使用:

var steamId = ['SomeSteamIDs']; 
var gameIds = [];
var games = [];

function getGames(){

  for (var i = 0; i < steamId.length; i++){
  var response = UrlFetchApp.fetch("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=**YourSteamKey**&steamid=" + steamId[i] + "&format=json&include_appinfo=1");//Steam URL. 
  Logger.log('Response Code: ' + response.getResponseCode());//Checks the request actually connected. 
  var data = JSON.parse(response.getContentText());//Gets the plaintext JSON response and converts it to an object.

      for (var n = 0; n < data.response.games.length; n++){//For the length of the games list
       var code = data.response.games[n].appid;
       var name = data.response.games[n].name;
       if (gameIds.indexOf(code) === -1){//if the AppID doesn't appear in the 'appId' array and sub-arrays
           gameIds[gameIds.length] = code;//then put it in the appId array for comparison
           games[games.length] = [code,name];// and add the AppId and Game to the games array for writting.
     };
  }; 
 }
   var range = sheet.getRange("A2:B" + (gameIds.length + 1));//+1 here to compensate for starting on line 2, instead of 1. 
   range.setValues(games);//Perform one write action

}

这对于编译所有 SteamID 所拥有的游戏主列表非常有效,但我很难找到一种方法来检查每个个人 ID 拥有哪些游戏,哪些不拥有。

最初,我尝试在运行“getGames”函数时向“games”数组添加“是/否”字符串,但我提出的任何解决方案都会丢失数据。如果我太早比较这些值,'gameIds' 数组不包含所有数据,因此 第一个 SteamID 会错过与 SteamID 拥有的最后一个。

如果我做得太晚,“data”变量仅包含它检查的最后 SteamID 的响应数据,因此我会错过检查第一个哪些游戏> SteamID 拥有。

我已经多次阅读 How to compare arrays in JavaScript? 上的答案,但我仍在尝试弄清楚是否可以使用它来解决我的问题。

有没有办法让我实现我想要的目标,最有效的方法是什么?

最佳答案

我会以不同的方式处理这个问题。我将保留一个 gameList 对象,其中包含 id 的游戏对象键,该键具有 name 属性,然后是 userList 属性,该属性是附加到每个游戏的用户数组。这将为您做一些事情。第一,您现在可以在恒定时间内查找游戏,而不是循环在数组中查找它(indexOf 就是这样做的)。第二,您现在拥有一个唯一的游戏列表(游戏对象的所有属性),其中包含一组用户 ID(谁拥有它们),以便于查找。这是我所描述的代码

var steamIds = [],
    gameList = {};

function getGames(){

    steamIds.forEach(function(steamId) {
        var response =  UrlFetchApp.fetch("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=**YourSteamKey**&steamid=" + steamId[i] + "&format=json&include_appinfo=1");//Steam URL. 
        Logger.log('Response Code: ' + response.getResponseCode());//Checks the request actually connected. 
        var data = JSON.parse(response.getContentText());//Gets the plaintext JSON response and converts it to an object.

        data.response.games.forEach(function(game) {//For the length of the games list
            var code = game.appid;
            var name = game.name;
            if (!gameList.hasOwnProperty(code)) {
                gameList[code] = {name: name, userList: [steamId]};       
            } else {
                gameList[code].userList.push(steamId);
            }
        });
    });
}

这是游戏列表完成后的最终结果示例

gameList : {
    123: {
        name: 'Some Game',
        userList: [80, 90, 52]
    },
    567: {
        name: 'Another Game',
        userList: [68]
    }
}

写入单元格会发生一些变化,但您的信息现在以一种可以轻松获取有关特定游戏或拥有该游戏的用户的信息的方式关联。

关于javascript - 何时比较 'for' 循环中的多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26208005/

相关文章:

javascript - 比较 Google Apps 脚本中的数组值

rest - Openid 和 RestFul API

Python Beautiful Soup 和 urllib.request - 如何通过 Steam 年龄检查

javascript - d3 没有严格亲子关系的旭日

google-apps-script - 在提交时获取当前响应

google-apps-script - Google Apps脚本更改Google工作表中特定单元格的背景颜色

java - 如何在使用 HtmlUnit 时 "get around"steam 登录?

Javascript - 将日期字符串重新格式化为 ISO8601

javascript - 如何在Flash CS5.5 Actionscript 3.0中创建警报弹出窗口?

javascript - 如何使用 moment js 查找季度剩余天数