Group Hash by values in ruby
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"]]}
Ruby hash group by value
I would do something like this:
student_marks.group_by { |k, v| v }.map { |k, v| [k, v.map(&:first)] }.to_h
#=> { 50 => ["Alex", "Matt"], 54 => ["Beth"]}
Ruby group hashes based on matching keys and store the value of non matching keys in an array
You can try grouping by key
and value
and then map the revenue
values:
foos
.group_by { |e| e.values_at(:key, :value) }
.map do |(key, value), values|
{ key: key, value: value, revenue: values.map { |e| e[:revenue] } }
end
# [{:key=>"Foo", :value=>1, :revenue=>[2, 4]}, {:key=>"Bar", :value=>2, :revenue=>[7]}, {:key=>"bar", :value=>2, :revenue=>[9]}, {:key=>"Zampa", :value=>4, :revenue=>[9]}]
i want to make a group Hash by keys and add values
I'd use the each_with_object
method.(key, value)
here is a deconstruction of each key/value pair like 93=>1
, hash
is an intermediate object to store the result.
data.each_with_object({}) do |(key, value), hash|
result_key =
case key
when 10..19 then 1
when 20..29 then 2
when 90..99 then 9
end
next if result_key.nil?
hash[result_key] ||= 0
hash[result_key] += value
end
For the provided input I got {9=>2, 2=>7856, 1=>267}
UPD
A shorter solution suggested by Holger Just and Stefan in the comments below.
data.each_with_object(Hash.new(0)) do |(key, value), hash|
hash[key / 10] += value
end
With Hash.new(0)
the initial object will be a hash with the default value 0
> hash = Hash.new(0)
=> {}
> hash[1]
=> 0
Ruby group hashes by value of key
array.group_by {|x| x['type']}
or if you want the symbol key things you could even
array.group_by {|x| "type_#{x['type']}".to_sym}
I think this best expresses "So basically each array of hashes would be grouped by the value of its type key, and then returned as a hash with an array for each type", even if it leaves the :type
key alone in the output hashes.
Ruby hashes group by multiple value
This seems to do what you want:
transformed_hash = initial_hash
.group_by { |x| x['name'] }
.map { |k, v| [k, v.group_by{ |x| x['folder'] }] }
.to_h
Ruby Hash Group by two keys
You're grouping the result after the first group, but the hash content remains the same, you can use map to create a new hash only with the values you need:
report['count_by_state'].group_by { |count| count['policy_state'] }
.transform_values do |val|
val.to_h { |hash| hash.values_at('day', 'count') }
end
# {
# 2=>{"2021-02-23"=>48, "2021-02-25"=>1},
# 3=>{"2021-02-23"=>5, "2021-02-25"=>4},
# 4=>{"2021-02-23"=>89, "2021-02-25"=>90}
# }
to_h
is the short version of map { |...| [x, y] }.to_h
, if your Ruby version doesn't support it yet, you can use that.
Group array of hashes by value and retain structure (hash) in Ruby
This should work:
hash.group_by { |k,v| v[:where] }.each { |_, v| v.map! { |array| { array[0] => array[1] } } }
Or with transform_values
hash.group_by { |k,v| v[:where] }.transform_values { |v| v.map { |array| {array[0] => array[1] } } }
https://ruby-doc.org/core-2.4.0/Hash.html#method-i-transform_values
Related Topics
Convert Ip Address to 32 Bit Integer in Ruby
Using $1, $2, etc. Global Variables Inside Method Definition
Ruby: Syntax for Defining a Constant Inside a Struct
Array#Rotate Equivalent in Ruby 1.8.7
Error: "Fatal: I Don't Handle Protocol ''Git' When Using Bundle Install
Rails Reload Dynamic Routes on Multiple Instances/Servers
How to Programmatically Convert Mp3 to an Itunes-Playable Aac/M4A File
Why Are My Rspec Tests Failing, But My App Is Working
Ruby Spreadsheet Row Background Color
Unexpected Keyword_End, Expecting $End (Syntaxerror)
Autoload Paths and Nested Services Classes Crash in Ruby
Finding the Date for a Given Week Number
How to Copy an Object with Presigned Url
Tinytds Error: Adaptive Server Connection Timed Out
How to Access Current_User Object in Model
How to Render a Layout Directly from Routes.Rb, Without a Controller