在CoffeeScript中,如何根据与变量匹配的另一个值从对象获取值?

时间:2016-08-03 17:46:14

标签: coffeescript slack hubot

我为我的团队Slack工作机器人,到目前为止,我的搜索空洞。我正在从API中检索JSON对象,并且返回正常。我想要做的是根据另一个属性是否匹配传递给函数的变量来获取对象元素的属性值。更详细地说,我尝试根据characterId检索classHash。我试过这个,但它没有用。它导致characterId未定义。

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

JSON对象如下:

"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}
                 }

我正在尝试获取characterId characters[].characterBase characters[].characterBase.classHash等于传入的2271682572。说我传递classHash作为classHash,我想返回与2305843009322984539匹配的characterId,在本例中为application.properties

任何人都可以帮助我或指出正确的方向吗?

2 个答案:

答案 0 :(得分:1)

假设characterID是唯一的:

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)

您将返回characterId,如果未找到匹配,则返回null。

实施例

getCharIdByClassHash(data.characters, 2271682572) # 2305843009322984539

修改

这假设你的'json'不是

"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": {

就像你写的那样但是

"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": {

我认为认为是你的意思。 Demo

答案 1 :(得分:1)

假设总有一个角色classHash。您可以过滤数组,弹出一个并获取其character

characterId = data
 .characters
 .filter (character) -> character.characterBase.classHash is classHash
 .pop()?.characterBase?.characterId

或使用数组comprehesion

[characterId] = (id for {characterBase: {characterId: id, classHash: hash}} in data.characters when hash is classHash)

不确定您的实际数据是什么,但这里是demo