Advertisement
Guest User

Untitled

a guest
Jan 28th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var key = "98C77A5A-330F-F74B-8946-8A24E66D50FD3FA1F803-7446-4B24-8CFC-C62D5F7679AF"
  2.  
  3. function getMaterials() {
  4.   var myUrl = "https://api.guildwars2.com/v2/account/materials?access_token=" + escape(key);
  5.   var jsonData = UrlFetchApp.fetch(myUrl);
  6.   var jsonString = jsonData.getContentText();
  7.   return jsonString;
  8. }
  9.  
  10. var data = getMaterials();
  11.  
  12. // Map each object by ID
  13. function getMaterialIdMap(data){
  14.   var materials = {};
  15.   for(var i = 0; i < data.length; i++ ){
  16.     var material = data[i];
  17.     if(material.hasOwnProperty('id')){ // Ensure we only get objects with ids
  18.       materials[material.id] = material;
  19.     }
  20.   }
  21.   return materials
  22. }
  23.  
  24. // Map just the count to id
  25. function getIdToCountMap(data){
  26.   var materials = {};
  27.   for(var i = 0; i < data.length; i++ ){
  28.     var material = data[i];
  29.     if(material.hasOwnProperty('id') && material.hasOwnProperty('count')){ // Ensure we only get objects with counts and ids
  30.       materials[material.id] = material.count;
  31.     }
  32.   }
  33.   return materials
  34. }
  35.  
  36.  
  37. var materialMap = getMaterialIdMap(data);
  38.  
  39.  
  40. // For just the counts
  41. var materialCountMap = getIdToCountMap(data);
  42.  
  43. // For the full object by id
  44. function countFromId (id){
  45.    return materialCountMap[id];
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement