I have an array like this:
[{:a=>"2017-01-01", :b=>"2", :c=>"1"}, {:a=>"2017-01-01", :b=>"2", :c=>"2"}, {:a=>"2017-01-02", :b=>"5", :c=>"1"}]
If, for example, I group the array with the key ":a", the result is:
p = y.group_by { |g| g[:a]}
=> {"2017-01-01"=>[{:a=>"2017-01-01", :b=>"2", :c=>"1"}, {:a=>"2017-01-01", :b=>"2", :c=>"2"}], "2017-01-02"=>[{:a=>"2017-01-02", :b=>"5", :c=>"1"}]}
Now I want to group for each ":a" the key ":b" like:
=> {"2017-01-01"=>["2" => [{:a => "2017-01-01", :b => "2", :c => "1" }], ... }
How can I do? Thanks in advance.
Edit:
multiple group by ... 1° group_by a, 2° group_by b the result of 1°, etc..
y.group_by {|g| g[:a]}.map do |k,v|
[
k, v.group_by { |d| d[:b] }.map do |p,q|
[p, q.group_by { |f| f[:c] }]
end.to_h
]
end.to_h
[:a=>...is not a valid Ruby object. Please edit to correct. 2. It's not obvious what...stands for. Please replace all...with the values you desire as output for the example. 3. When you give an example (generally) assign a variable to each input object (e.g.,arr = [{:a=>...) so that readers can refer to those variables (here just one) in answers and comments without having to define them.