访问由permaiters在哈希内的数组中的哈希

时间:2013-12-03 00:42:56

标签: ruby-on-rails ruby json hash

我想获得第一个射手的名字。这个问题是数据在Hash中,它位于下面哈希的一个数组中,然后我需要测试它以查看它是否是一个目标。

有什么想法吗?

数据:

[
   {
      "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
         }
      ]
   }
]

2 个答案:

答案 0 :(得分: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时,循环在找到第一个目标后结束。

答案 1 :(得分:0)

这就是我最终解决的问题:

任何重构想法?

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