Ruby group hashes by value of key
Asked Answered
T

4

19

I have an array, which is output by a map/reduce method performed by MongoDB, it looks something like this:

[{"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>299.0}, 
{"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>244.0}, 
{"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>1.0, "count"=>204.0}, 
{"minute"=>45.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>510.0}, 
{"minute"=>45.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>437.0}, 
{"minute"=>0.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>469.0}, 
{"minute"=>0.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>477.0}, 
{"minute"=>15.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>481.0}, 
{"minute"=>15.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>401.0}, 
{"minute"=>30.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>468.0}, 
{"minute"=>30.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>448.0}, 
{"minute"=>45.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>485.0}, 
{"minute"=>45.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>518.0}] 

You'll notice that there are three distinct values for type, in this case 0, 1, and 2, now want to do is group this array of hashes by the value its type key, so for example this array would end out looking like:

{
  :type_0 => [
    {"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>299.0}, 
    {"minute"=>45.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>510.0}, 
    {"minute"=>0.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>469.0}, 
    {"minute"=>15.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>481.0}, 
    {"minute"=>30.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>468.0}, 
    {"minute"=>45.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>485.0}
  ],

  :type_1 => [
    {"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>204.0}
  ],

  :type_10 => [
    {"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>244.0}, 
    {"minute"=>45.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>437.0},
    {"minute"=>0.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>477.0}, 
    {"minute"=>15.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>401.0}, 
    {"minute"=>30.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>448.0}, 
    {"minute"=>45.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>518.0}
  ]
} 

so I know these example arrays are really large, but I think it may be a more simple problem than I'm making it out to be

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, any help at all would be really really helpful, even just some helpful hints would be greatly appreciated.

Transcendence answered 6/10, 2011 at 3:24 Comment(1)
possible duplicate of Best way to split arrays into multiple small arrays in rubyFaletti
H
39
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.

Halleyhalli answered 6/10, 2011 at 3:44 Comment(2)
it doesn't produce the output in the question, and doesn't work in Ruby 1.8Iritis
This will group, but it doesn't delete the 'type' in the response. I don't mind that, as it's simple, but it doesn't answer the question, tbh.Sloat
A
2
by_type = {}

a.each do |h|
   type = h.delete("type").to_s
   # type = ("type_" + type ).to_sym

   by_type[ type ] ||= []
   by_type[ type ] << h      # note: h is modified, without "type" key

end

Note: slightly different hash keys here, i used the type values directly as the key

if you have to have the hash-keys as in your example, you can add the line that is commented out.


P.S.: I just saw Tapio's solution -- it is very nice and short! Note that it only works with Ruby >= 1.9

Atween answered 6/10, 2011 at 3:34 Comment(3)
why not just a.group_by {|x| x['type']} ?Halleyhalli
In that it doesn't remove the 'type' key? I don't think that would really matter, would it?Halleyhalli
@Tapio: in his example, he was expecting that the "type" key is removed from the hashes along the way... Yes, I agree, doesn't really matter .. group_by() is new and yummy, thanks! +1Atween
B
2

Something like this perhaps?

mangled = a.group_by { |h| h['type'].to_i }.each_with_object({ }) do |(k,v), memo|
    tk = ('type_' + k.to_s).to_sym
    memo[tk] = v.map { |h| h = h.dup; h.delete('type'); h }
end

Or if you don't care about preserving the original data:

mangled = a.group_by { |h| h['type'].to_i }.each_with_object({ }) do |(k,v), memo|
    tk = ('type_' + k.to_s).to_sym
    memo[tk] = v.map { |h| h.delete('type'); h } # Drop the h.dup in here
end
Burmese answered 6/10, 2011 at 3:39 Comment(0)
B
2

group_by collects an enumerable into sets, grouped by the result of a block. You are not constrained to simply get the key's value in this block, so if you would like to omit the 'type' in those sets you can do it, like in:

array.group_by {|x| "type_#{x.delete('type').to_i}".to_sym}

This will result exactly into what you asked.

Advanced: This goes a little out of scope of the question, but if you want to preserve the original array, you must duplicate every object inside it. This will do the trick:

array.map(&:dup).group_by {|x| "type_#{x.delete('type').to_i}".to_sym}
Byron answered 3/9, 2015 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.