Group Hash by values in ruby

后端 未结 5 1195
广开言路
广开言路 2021-01-11 10:49

I have a hash in ruby which looks something like this:

{
  \"admin_milestones\"=>\"1\",
  \"users_milestones\"=>\"0\",
  \"admin_goals\"=>\"1\",
  \         


        
相关标签:
5条回答
  • 2021-01-11 11:19

    Just Hash.select:

    h1.select { |key, value| value == '0' } #=> {"users_milestones"=>"0", "users_goals"=>"0", ...}
    h1.select { |key, value| value == '1' } #=> {"admin_milestones"=>"1", "admin_goals"=>"1", ...}
    

    The return value depends on your Ruby version. Ruby 1.8 returns a array of arrays, whereas Ruby 1.9 returns a hash like in the example above.

    0 讨论(0)
  • 2021-01-11 11:21

    You should use group_by on the keys arrays and use the value as the grouping element:

    h1 = {
      "admin_milestones"=>"1",
      "users_milestones"=>"0",
      "admin_goals"=>"1",
      "users_goals"=>"0", 
      "admin_tasks"=>"1", 
      "users_tasks"=>"0",
      "admin_messages"=>"1",
      "users_messages"=>"0",
      "admin_meetings"=>"1",
      "users_meetings"=>"0"
    }
    
    # group_by on the keys, then use the value from the hash as bucket
    h2 = h1.keys.group_by { |k| h1[k] }
    
    puts h2.inspect
    

    Returns a hash from value to array of keys:

    {
        "1" => [
            [0] "admin_milestones",
            [1] "admin_goals",
            [2] "admin_tasks",
            [3] "admin_messages",
            [4] "admin_meetings"
        ],
        "0" => [
            [0] "users_milestones",
            [1] "users_goals",
            [2] "users_tasks",
            [3] "users_messages",
            [4] "users_meetings"
        ]
    }
    
    0 讨论(0)
  • 2021-01-11 11:29

    If you want an array as answer the cleanest solution is the partition method.

    zeros, ones = my_hash.partition{|key, val| val == '0'}
    
    0 讨论(0)
  • 2021-01-11 11:32

    Similar with https://stackoverflow.com/a/56164608/14718545 you can use group_by but with then, in this case, you will avoid instantiating an extra variable.

    {
      "admin_milestones" => "1",
      "users_milestones" => "0",
      "admin_goals" => "1",
      "users_goals" => "0",
      "admin_tasks" => "1",
      "users_tasks" => "0",
      "admin_messages" => "1",
      "users_messages" => "0",
      "admin_meetings" => "1",
      "users_meetings" => "0"
    }.then { |h| h.keys.group_by { |k| h[k] } }
    
    {"1"=>["admin_milestones", "admin_goals", "admin_tasks", "admin_messages", "admin_meetings"],
     "0"=>["users_milestones", "users_goals", "users_tasks", "users_messages", "users_meetings"]}
    
    0 讨论(0)
  • 2021-01-11 11:39

    You can group hash by its value:

    h1 = {
      "admin_milestones"=>"1",
      "users_milestones"=>"0",
      "admin_goals"=>"1",
      "users_goals"=>"0", 
      "admin_tasks"=>"1", 
      "users_tasks"=>"0",
      "admin_messages"=>"1",
      "users_messages"=>"0",
      "admin_meetings"=>"1",
      "users_meetings"=>"0"
    }
    
    h2 = h1.group_by{|k,v| v}
    

    It will produce a hash grouped by its values like this:

    h2 = {"1"=>[["admin_milestones", "1"], ["admin_goals", "1"], ["admin_tasks", "1"], ["admin_messages", "1"], ["admin_meetings", "1"]], 
    "0"=>[["users_milestones", "0"], ["users_goals", "0"], ["users_tasks", "0"], ["users_messages", "0"], ["users_meetings", "0"]]} 
    
    0 讨论(0)
提交回复
热议问题