【问题标题】:In CoffeeScript, how do I get a value from an object based on another value matching a variable?在 CoffeeScript 中,如何根据与变量匹配的另一个值从对象中获取值?
【发布时间】:2016-12-09 13:53:57
【问题描述】:

我正在为我团队的 Slack 开发一个机器人,但到目前为止我的搜索结果都是空的。我正在从 API 检索 JSON 对象,并且它返回正常。我要做的是根据另一个属性是否与传递给函数的变量匹配来获取对象元素的属性值。更详细地说,我正在尝试基于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[].characterBase 的characterId,其中characters[].characterBase.classHash 等于传入的那个。假设我传入2271682572 作为classHash,我想返回与@匹配的字符ID 987654330@,在本例中为2305843009322984539

谁能帮助我或指出正确的方向?

【问题讨论】:

  • 如果您的意思是根据变量classHash 的字符串value 键入characterBase,那么您需要使用[] 属性访问器而不是@ 987654335@ 像这样:data.characters[0].characterBase[classHash]。否则,不清楚你的问题是什么。
  • 那行不通。我将添加我正在处理的 JSON 对象的一部分。
  • 请解释 a) 你的代码在做什么和 b) 你想让它做什么。从您问题中的描述来看,它根本不清楚。
  • 我已经更新了问题。希望它能增加更多的清晰度......我很难弄清楚措辞。

标签: coffeescript slack hubot


【解决方案1】:

假设characterIDs 是唯一的:

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.

【讨论】:

  • @dkiefer 我现在看到了问题,您的对象数组缺少右括号。
  • 它有右大括号,我只是缩短了从 API 返回的实际对象(大量数据)。
  • @dkiefer 好吧,那么 Yury 和我的解决方案都应该可以正常工作,除非数据结构不同或冲突哈希实际上是字符串而不是数字或类似的东西。
  • 添加了一个演示。运行时提醒 characterId。
  • 你是个天才。我什至没有考虑需要将其转换为数字的事实,因为它是 JSON 字符串的一部分。非常感谢!
【解决方案2】:

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

【讨论】:

  • 我更喜欢reduce 的唯一原因是失败案例。
  • 理解例子处理这种情况。如果没有匹配项,characterId 将是未定义的。如果多个字符的大小写相同,则为第一个匹配项。
  • 我尝试了这两种方法,但都没有奏效。第一个导致ERROR TypeError: Cannot read property 'characterBase' of undefined,第二个导致ERROR SyntaxError: Unexpected token。我对 CoffeeScript 非常陌生,所以我不知道自己做错了什么。
  • 第一种方法更新后我试过了,现在characterId未定义
  • @YuryTarabanko 很确定问题出在对象数组上,它的格式不正确(至少就我们答案中的假设而言)。请参阅我的答案的编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 2016-02-12
  • 1970-01-01
  • 2017-09-07
  • 2022-06-29
相关资源
最近更新 更多