M
M
Master Ruby2021-11-25 22:22:23
ruby
Master Ruby, 2021-11-25 22:22:23

How to get values ​​of only certain keys in hash?

data = {
  "player" => {        
      "PlayerId" => "16919853080087905990201061748435842063573506113420581758304706335291498269520",
      "FullName" => "Robert Lewandowski",
      "DateOfBirth" => "21/8/1988",
      "TeamId" => "bayern-munchen-munchen",
      "Age" => 33,
      "Slug" => "robert-lewandowski",
      "DisplayName" => "Robert Lewandowski",
      "Position" => "Forward",
      "PictureUrl" => "https://assets.sorare.com/seasonpicture/d30ad14a-0c0a-4ca1-a88a-87c0e2096037/picture/b41df4f95b7d1e9001b33be0248a9f19.png",
      "TrimmedPictureUrl" => "https://assets.sorare.com/seasonpicture/d30ad14a-0c0a-4ca1-a88a-87c0e2096037/picture/trimmed-765ac725f9df44e1c5dc435cf7db73cc.png",
      "Height" => 184,
      "Weight" => 80,
      "BirthDate" => "1988-08-21T00:00:00Z",
      "Avatar" => "https://assets.sorare.com/seasonpicture/d30ad14a-0c0a-4ca1-a88a-87c0e2096037/picture/avatar-3d48e1f60ca1bae3c4924e45240e0ab6.png",
      "Licensed" => false,
      "SubscriptionsCount" => 138918,
      "PlayingStatus" => "starter",
      "SquaredPictureUrl" => "https://assets.sorare.com/seasonpicture/d30ad14a-0c0a-4ca1-a88a-87c0e2096037/picture/squared-15184c2a83b47881814394e99aaeaf3e.png",
      "NationalTeam" => "poland",
      "MatchName" => "R. Lewandowski",
      "JerseyNumber" => 9
    },

    "team" => {
  
      "TeamId" => "bayern-munchen-munchen",
      "DisplayName"=> "FC Bayern München",
      "League"=> "Bundesliga",
      "Licensed"=> false,
      "ClubId"=> "null",
      "Code"=> "FCB",
      "City"=> "null",
      "Country"=> "null",
      "TeamName"=> "null",
      "PictureUrl"=> "https://assets.sorare.com/club/157dab87-1c6c-4db3-942e-f5468b65ac91/picture/e00fbb7fe56b81c82da15f8425f0df04.png",
      "LeagueSlug"=> "bundesliga-de",
      "EligiblePlayers"=> 26,
      "International"=> false,
      "ShortName"=> "Bayern München",
      "ClubName"=> "Bayern München"
    },
}
puts data['player']['Age']

How to get values ​​immediately using each:
['player']=>['Age'], 
['player']=>['Position'], 
['team']=>['LeagueSlug'],

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene Burmakin, 2021-12-14
@Dunaevlad

It is also possible without `.each`:

{
  'Age' => data.dig('player', 'Age'),
  'Position' => data.dig('player', 'Position'),
  'League' => data.dig('team', 'LeagueSlug'),
}

Something like that

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question