【问题标题】:Accessing a Hash within an Array that is within a hash by permaiters [closed]通过permaiters访问哈希内的数组中的哈希[关闭]
【发布时间】:2023-03-31 04:49:01
【问题描述】:

我想知道第一个射手的名字。这样做的问题是数据位于下面哈希中的数组中的哈希中,然后我需要对其进行测试以查看它是否是目标。

有什么想法吗?

数据:

[
   {
      "id":1474387,
      "date":"2013-11-24 16:00:00",
      "competition_id":831975,
      "competition":"England Premier League",
      "group":"",
      "home_id":8344,
      "home":"Cardiff City",
      "homeshort":"Cardiff",
      "homepath":"cardiff-city",
      "away_id":10260,
      "away":"Manchester United",
      "awayshort":"Man Utd",
      "awaypath":"manchester-united",
      "status":"Finished",
      "halftime":[
         1,
         2
      ],
      "fulltime":[
         2,
         2
      ],
      "extratime":[
         0,
         0
      ],
      "penalties":[
         0,
         0
      ],
      "incidents":[
         {
            "id":2670186,
            "type":"Yellow",
            "goaltype":null,
            "team_id":10260,
            "team":"Manchester United",
            "teamshort":"Man Utd",
            "teampath":"manchester-united",
            "player_id":30829,
            "player":"Wayne Rooney",
            "playershort":"W. Rooney",
            "minute":8
         },
         {
            "id":2670226,
            "type":"Goal",
            "goaltype":"Regular goal",
            "team_id":10260,
            "team":"Manchester United",
            "teamshort":"Man Utd",
            "teampath":"manchester-united",
            "player_id":30829,
            "player":"Wayne Rooney",
            "playershort":"W. Rooney",
            "minute":15
         },
         {
            "id":2670320,
            "type":"Goal",
            "goaltype":"Regular goal",
            "team_id":8344,
            "team":"Cardiff City",
            "teamshort":"Cardiff",
            "teampath":"cardiff-city",
            "player_id":24157,
            "player":"Fraizer Campbell",
            "playershort":"F. Campbell",
            "minute":33
         },
         {
            "id":2670367,
            "type":"Goal",
            "goaltype":"Regular goal",
            "team_id":10260,
            "team":"Manchester United",
            "teamshort":"Man Utd",
            "teampath":"manchester-united",
            "player_id":32569,
            "player":"Patrice Evra",
            "playershort":"P. Evra",
            "minute":45
         },
         {
            "id":2670471,
            "type":"Yellow",
            "goaltype":null,
            "team_id":8344,
            "team":"Cardiff City",
            "teamshort":"Cardiff",
            "teampath":"cardiff-city",
            "player_id":176889,
            "player":"Steven Caulker",
            "playershort":"S. Caulker",
            "minute":51
         },
         {
            "id":2670485,
            "type":"Yellow",
            "goaltype":null,
            "team_id":8344,
            "team":"Cardiff City",
            "teamshort":"Cardiff",
            "teampath":"cardiff-city",
            "player_id":23806,
            "player":"Peter Whittingham",
            "playershort":"P. Whittingham",
            "minute":55
         },
         {
            "id":2670648,
            "type":"Yellow",
            "goaltype":null,
            "team_id":10260,
            "team":"Manchester United",
            "teamshort":"Man Utd",
            "teampath":"manchester-united",
            "player_id":160713,
            "player":"Tom Cleverley",
            "playershort":"T. Cleverley",
            "minute":87
         },
         {
            "id":2670676,
            "type":"Goal",
            "goaltype":"Regular goal",
            "team_id":8344,
            "team":"Cardiff City",
            "teamshort":"Cardiff",
            "teampath":"cardiff-city",
            "player_id":197910,
            "player":"Bo-Kyung Kim",
            "playershort":"BK. Kim",
            "minute":90
         },
         {
            "id":2670718,
            "type":"Yellow",
            "goaltype":null,
            "team_id":8344,
            "team":"Cardiff City",
            "teamshort":"Cardiff",
            "teampath":"cardiff-city",
            "player_id":159882,
            "player":"Kevin Theophile Catherine",
            "playershort":"K. Theophile Catherine",
            "minute":90
         },
         {
            "id":2670720,
            "type":"Yellow",
            "goaltype":null,
            "team_id":8344,
            "team":"Cardiff City",
            "teamshort":"Cardiff",
            "teampath":"cardiff-city",
            "player_id":197910,
            "player":"Bo-Kyung Kim",
            "playershort":"BK. Kim",
            "minute":90
         }
      ]
   }
]

【问题讨论】:

  • 您需要在我要向您展示的另一个循环中使用 .each 循环,但该代码很难阅读。可以链接控制器吗?

标签: ruby-on-rails ruby json hash


【解决方案1】:

如果你有一场比赛的数据,你可以像这样得到第一个进球者:

goal_incident = match['incidents'].detect{|i| i['type'] == 'Goal'}
if goal_incident
  puts "First goal scorer was #{goal_incident['playershort']}"
else
  puts "There were no goals :("
end

您的解决方案还可以,但速度有点慢。当您使用select 时,ruby 必须遍历整个数组,如果您只对第一个目标感兴趣,这是不必要的。当您使用detect 时,循环在找到第一个目标后结束。

【讨论】:

    【解决方案2】:

    最后我是这样解决的:

    有什么重构的想法吗?

    json.each do |result|
      score = result["fulltime"]
      goals = result['incidents'].select do |incidents|
        incidents['goaltype'] != nil
      end
    
      if goals.count >= 1
        first_goalscorer_player_name = goals.first['playershort'] 
      else 
        first_goalscorer_player_name = "No Goal Scorer"
      end
    

    【讨论】:

    • 编辑后看起来好多了,我相应地更新了我的答案并添加了重构说明。
    猜你喜欢
    • 2013-03-22
    • 2013-06-06
    • 2014-05-06
    • 2021-01-18
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    相关资源
    最近更新 更多