Search code examples
coffeescriptslackhubot

In CoffeeScript, how do I get a value from an object based on another value matching a variable?


I'm working on a bot for my team's Slack and so far my searches have come up empty. I'm retrieving a JSON object from an API, and it is returning fine. What I'm trying to do is get the value of the property of an element of the object based on if another property matches a variable being passed in to the function. In more detail, I'm trying to retrieve the characterId based on the classHash. I'm trying this, but it's not working. It results in characterId being undefined.

if data.characters[0].characterBase.classHash is classHash
  characterId = data.characters[0].characterBase.characterId
else if data.characters[1].characterBase.classHash is classHash
  characterId = data.characters[1].characterBase.characterId
else if data.characters[2].characterBase.classHash is classHash
  characterId = data.characters[2].characterBase.characterId

The JSON object is as follows:

"characters": [
                {
                    "characterBase": {
                        "membershipId": "4611686018451933949",
                        "membershipType": 2,
                        "characterId": "2305843009322984539",
                        "dateLastPlayed": "2016-07-31T02:53:56Z",
                        "minutesPlayedThisSession": "130",
                        "minutesPlayedTotal": "40010",
                        "powerLevel": 335,
                        "raceHash": 3887404748,
                        "genderHash": 3111576190,
                        "classHash": 2271682572}
                },
                {
                    "characterBase": {
                        "membershipId": "4611686018451933949",
                        "membershipType": 2,
                        "characterId": "2305843009340115467",
                        "dateLastPlayed": "2016-06-22T00:28:46Z",
                        "minutesPlayedThisSession": "108",
                        "minutesPlayedTotal": "11113",
                        "powerLevel": 333,
                        "raceHash": 2803282938,
                        "genderHash": 2204441813,
                        "classHash": 671679327}
                },
                {
                    "characterBase": {
                        "membershipId": "4611686018451933949",
                        "membershipType": 2,
                        "characterId": "2305843009327058547",
                        "dateLastPlayed": "2016-06-12T23:30:01Z",
                        "minutesPlayedThisSession": "62",
                        "minutesPlayedTotal": "9831",
                        "powerLevel": 334,
                        "raceHash": 898834093,
                        "genderHash": 3111576190,
                        "classHash": 3655393761}
                 }

I am trying to get the characterId of characters[].characterBase where the characters[].characterBase.classHash equals the one passed in. Say I pass in 2271682572 as the classHash, I want to return the characterId that matches that classHash, in this case 2305843009322984539

Can anyone help me or point me in the right direction?


Solution

  • Assuming that characterIDs are unique:

    getCharIdByClassHash = (characters, classHash) ->
      characters.reduce(((result, char) ->
        result or # if we've already found the result, pass it thru
          (if char.characterBase.classHash is classHash
             char.characterBase.characterId
           else
             null)
      ), null)
    

    You will either get back the characterId, or null if a match isn't found.

    Example:

    getCharIdByClassHash(data.characters, 2271682572) # 2305843009322984539
    

    EDIT

    this assumes your 'json' is NOT

    "characters": [
                    {
                        "characterBase": {
                            "membershipId": "4611686018451933949",
                            "membershipType": 2,
                            "characterId": "2305843009322984539",
                            "dateLastPlayed": "2016-07-31T02:53:56Z",
                            "minutesPlayedThisSession": "130",
                            "minutesPlayedTotal": "40010",
                            "powerLevel": 335,
                            "raceHash": 3887404748,
                            "genderHash": 3111576190,
                            "classHash": 2271682572,
                    {
                        "characterBase": {
                            "membershipId": "4611686018451933949",
                            "membershipType": 2,
                            "characterId": "2305843009340115467",
                            "dateLastPlayed": "2016-06-22T00:28:46Z",
                            "minutesPlayedThisSession": "108",
                            "minutesPlayedTotal": "11113",
                            "powerLevel": 333,
                            "raceHash": 2803282938,
                            "genderHash": 2204441813,
                            "classHash": 671679327,
                    {
                        "characterBase": {
    

    Like you wrote it but is

    "characters": [
                    {
                        "characterBase": {
                            "membershipId": "4611686018451933949",
                            "membershipType": 2,
                            "characterId": "2305843009322984539",
                            "dateLastPlayed": "2016-07-31T02:53:56Z",
                            "minutesPlayedThisSession": "130",
                            "minutesPlayedTotal": "40010",
                            "powerLevel": 335,
                            "raceHash": 3887404748,
                            "genderHash": 3111576190,
                            "classHash": 2271682572
                        }
                    }, {
                        "characterBase": {
                            "membershipId": "4611686018451933949",
                            "membershipType": 2,
                            "characterId": "2305843009340115467",
                            "dateLastPlayed": "2016-06-22T00:28:46Z",
                            "minutesPlayedThisSession": "108",
                            "minutesPlayedTotal": "11113",
                            "powerLevel": 333,
                            "raceHash": 2803282938,
                            "genderHash": 2204441813,
                            "classHash": 671679327
                        }
                    }, {
                        "characterBase": {
    

    Which is what I think you meant. Demo.