javascript - When to compare several Arrays in a 'for' loop -



javascript - When to compare several Arrays in a 'for' loop -

i'm still quite new javascript , google apps script, , i'm attempting create script takes friends steam ids, loops on owned games, lists them spreadsheet, , displays if owns game or not.

i've achieved first part, looping on of owned games each id , adding them array if don't exist in array works using:

var steamid = ['somesteamids']; var gameids = []; var games = []; function getgames(){ (var = 0; < 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 request connected. var info = json.parse(response.getcontenttext());//gets plaintext json response , converts object. (var n = 0; n < data.response.games.length; n++){//for length of games list var code = data.response.games[n].appid; var name = data.response.games[n].name; if (gameids.indexof(code) === -1){//if appid doesn't appear in 'appid' array , sub-arrays gameids[gameids.length] = code;//then set in appid array comparing games[games.length] = [code,name];// , add together appid , game games array writting. }; }; } var range = sheet.getrange("a2:b" + (gameids.length + 1));//+1 here compensate starting on line 2, instead of 1. range.setvalues(games);//perform 1 write action }

this works in compiling master list of games owned across steamids, i'm having difficulty in finding way of checking off games owned each individual id, , not.

initially experimenting adding 'yes/no' string 'games' array when running 'getgames' function, solution come looses data. if compare values early, 'gameids' array doesn't contain of data, first steamid misses out on comparing against games last steamid owns.

if late, 'data' variable contains response info last steamid checked, miss out on checking games first steamid owns.

i've read reply @ comparing 2 arrays in javascript several times now, i'm still trying figure out if can utilize solve issue.

is there way me accomplish i'm looking for, , efficient way?

i approach bit differently. maintain object of gamelist game object keys id have name property , userlist property array of users attached each game. few things you. one, can lookup game in constant time instead of looping find in array (which indexof does). two, have unique list of games (all properties of games object) array of user ids (who owns them) easy lookup. here's code of i'm describing

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 request connected. var info = json.parse(response.getcontenttext());//gets plaintext json response , converts object. data.response.games.foreach(function(game) {//for length of 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); } }); }); }

here's , illustration of end result of gamelist when it's done

gamelist : { 123: { name: 'some game', userlist: [80, 90, 52] }, 567: { name: 'another game', userlist: [68] } }

writing cells alter bit info associated in way makes easy info particular game or users own it.

javascript google-apps-script steam

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -