Search code examples
parsingdiscord.jssteam

How do I parse this output?


Im trying to output just the "name:" element if the "achieved:" is true. This is using the SteamAPI.

Its basically getting the data and storing it to the "achievements" variable.

steam.getUserAchievements(id, config.app).then(achievements => {

I tried doing something such as achievements.name but that didn't work.

Any ideas?

PlayerAchievements {
  steamID: '76561198170684483',
  gameName: 'Deadside',
  achievements: [
    Achievement {
      api: 'DS_FirstStep',
      name: 'First step',
      description: 'Make you first step in game',
      achieved: true,
      unlockTime: 1598449807
    },
    Achievement {
      api: 'DS_Weap_FirstBlood',
      name: 'First blood',
      description: 'Kill an enemy',
      achieved: true,
      unlockTime: 1598451567
    },
    Achievement {
      api: 'DS_Weap_SteelArms',
      name: 'Cold Steel',
      description: 'Kill 10 AI bots with melee weapons',
      achieved: false,
      unlockTime: 0
    },
    Achievement {
      api: 'DS_Weap_Gun',
      name: 'Gunslinger',
      description: 'Kill 100 AI bots with Pistols',
      achieved: false,
      unlockTime: 0
    },
    Achievement {
      api: 'DS_Weap_SMG',
      name: 'Need for Lead',
      description: 'Kill 100 AI bots with SMGs',
      achieved: false,
      unlockTime: 0
    },
    Achievement {
      api: 'DS_Weap_AR',
      name: 'Lead Storm',
      description: 'Kill 100 AI bots with Assault Rifles',
      achieved: true,
      unlockTime: 1598737115
    },
    Achievement {
      api: 'DS_Weap_ShotGun',
      name: 'Son of a Shotgun',
      description: 'Kill 100 AI bots with Shotguns',
      achieved: true,
      unlockTime: 1598755630
    },
    Achievement {
      api: 'DS_Weap_Snip',
      name: 'Rifleman',
      description: 'Kill 100 AI bots with Rifles',
      achieved: false,
      unlockTime: 0
    },
    Achievement {
      api: 'DS_Weap_Gren',
      name: 'Hottest Potato',
      description: 'Kill 10 AI bots with Grenades',
      achieved: false,
      unlockTime: 0
    },
    Achievement {
      api: 'DS_Weap_Headshot',
      name: 'Headshot!',
      description: 'Kill 50 AI bots with headshot',
      achieved: true,
      unlockTime: 1598801600
    },
    Achievement {
      api: 'DS_Weap_LastBullet',
      name: 'Last bullet',
      description: 'Kill 10 AI bots with the last bullet in a clip',
      achieved: true,
      unlockTime: 1598826382
    },
    Achievement {
      api: 'DS_Weap_KillPlayer',
      name: 'There can only One',
      description: 'Kill 10 players',
      achieved: false,
      unlockTime: 0
    },
    Achievement {
      api: 'DS_Weap_AllWeapons',
      name: 'Full Arsenal',
      description: 'Kill one AI bot with each weapon',
      achieved: false,
      unlockTime: 0
    },
    Achievement {
      api: 'DS_MF_Any',
      name: 'Modder',
      description: 'Put any mod onto a weapon',
      achieved: true,
      unlockTime: 1598456376
    },
    Achievement {
      api: 'DS_Craft_Any',
      name: 'Mr. Fix-It',
      description: 'Craft any item',
      achieved: true,
      unlockTime: 1598450912
    },
    Achievement {
      api: 'DS_Craft_Base',
      name: "A man's home is his castle",
      description: 'Build your first base',
      achieved: true,
      unlockTime: 1598737891
    },
    Achievement {
      api: 'DS_Craft_BreakUp',
      name: 'Field Doctor',
      description: 'Tear any item to rags',
      achieved: true,
      unlockTime: 1598450049
    },
    Achievement {
      api: 'DS_Craft_Lumberjack',
      name: 'Lumberjack',
      description: 'Chop 100 logs',
      achieved: false,
      unlockTime: 0
    },
    Achievement {
      api: 'DS_Trade_Any',
      name: 'Trader',
      description: 'Buy or sell 100 items',
      achieved: true,
      unlockTime: 1598458913
    },
    Achievement {
      api: 'DS_FoodWater_Any',
      name: 'Gourmet',
      description: 'Consume all kinds of food and drinks',
      achieved: false,
      unlockTime: 0
    }
  ]
}

Solution

  • From the code which you provided we can see that PlayerAchievements class has an Array achievements which contains an Achievement class.

    You can do the following in order to get the achievement name -

    steam.getUserAchievements(id, config.app).then(achievements => {
    console.log(achievements.achievements[0].name)
    })
    

    it will log the first achievement name similarly you can access other achievements also as it is an array.