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

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

我正在为我团队的 Slack 开发一个机器人,到目前为止我的搜索结果是空的。我正在从 API 中检索 JSON 对象,它 return 没问题。我想要做的是根据另一个 属性 是否与传递给函数的变量匹配来获取对象元素的 属性 的值。更详细地说,我正在尝试根据 classHash 检索 characterId。我正在尝试这个,但它不起作用。它导致 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}
                 }

我正在尝试获取 characters[].characterBasecharacterId,其中 characters[].characterBase.classHash 等于传入的值。假设我传入 2271682572 作为 classHash,我想 return 与 classHash 匹配的 characterId,在本例中为 2305843009322984539

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

假设 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.

假设总有一个字符带有classHash。你可以过滤数组,弹出一个并得到它的 character

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

或者使用数组推导

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

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