【问题标题】:When to compare several Arrays in a 'for' loop何时在“for”循环中比较多个数组
【发布时间】:2023-04-05 18:09:01
【问题描述】:

我对 Javascript 和 Google Apps 脚本还很陌生,我正在尝试创建一个脚本,它可以获取您朋友的 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”函数时将“是/否”字符串添加到“游戏”数组中,但我提出的任何解决方案都会丢失数据。如果我过早比较这些值,'gameIds' 数组不会包含所有数据,因此 first SteamID 错过了与 last SteamID 拥有。

如果我做得太晚,“数据”变量只包含它检查的 last SteamID 的响应数据,所以我错过了检查第一个的游戏> SteamID 拥有。

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

有没有办法让我实现我正在寻找的东西,最有效的方法是什么?

【问题讨论】:

    标签: javascript google-apps-script steam


    【解决方案1】:

    我会以不同的方式处理这个问题。我会通过 id 保留一个 gameList 对象,该对象键具有 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 完成后的最终结果示例

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

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

    【讨论】:

    • 谢谢,我已将此标记为“已接受”,因为它解决了问题,并为我带来了一两个新东西。我能够添加一个函数,其中 '(gameList[i].userList.indexOf(steamId) !== -1)' 它会记录游戏是否拥有。这给我带来了一个全新的问题,即我将如何记录/显示数据,但我认为我更近了一步。
    猜你喜欢
    • 2014-08-26
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多