Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby group hashes by value of key
    primarykey
    data
    text
    <p>I have an array, which is output by a map/reduce method performed by MongoDB, it looks something like this:</p> <pre><code>[{"minute"=&gt;30.0, "hour"=&gt;15.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;0.0, "count"=&gt;299.0}, {"minute"=&gt;30.0, "hour"=&gt;15.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;10.0, "count"=&gt;244.0}, {"minute"=&gt;30.0, "hour"=&gt;15.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;1.0, "count"=&gt;204.0}, {"minute"=&gt;45.0, "hour"=&gt;15.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;0.0, "count"=&gt;510.0}, {"minute"=&gt;45.0, "hour"=&gt;15.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;10.0, "count"=&gt;437.0}, {"minute"=&gt;0.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;0.0, "count"=&gt;469.0}, {"minute"=&gt;0.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;10.0, "count"=&gt;477.0}, {"minute"=&gt;15.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;0.0, "count"=&gt;481.0}, {"minute"=&gt;15.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;10.0, "count"=&gt;401.0}, {"minute"=&gt;30.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;0.0, "count"=&gt;468.0}, {"minute"=&gt;30.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;10.0, "count"=&gt;448.0}, {"minute"=&gt;45.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;0.0, "count"=&gt;485.0}, {"minute"=&gt;45.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "type"=&gt;10.0, "count"=&gt;518.0}] </code></pre> <p>You'll notice that there are three distinct values for <code>type</code>, in this case <code>0</code>, <code>1</code>, and <code>2</code>, now want to do is group this array of hashes by the value its <code>type</code> key, so for example this array would end out looking like:</p> <pre><code>{ :type_0 =&gt; [ {"minute"=&gt;30.0, "hour"=&gt;15.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;299.0}, {"minute"=&gt;45.0, "hour"=&gt;15.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;510.0}, {"minute"=&gt;0.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;469.0}, {"minute"=&gt;15.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;481.0}, {"minute"=&gt;30.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;468.0}, {"minute"=&gt;45.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;485.0} ], :type_1 =&gt; [ {"minute"=&gt;30.0, "hour"=&gt;15.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;204.0} ], :type_10 =&gt; [ {"minute"=&gt;30.0, "hour"=&gt;15.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;244.0}, {"minute"=&gt;45.0, "hour"=&gt;15.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;437.0}, {"minute"=&gt;0.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;477.0}, {"minute"=&gt;15.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;401.0}, {"minute"=&gt;30.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;448.0}, {"minute"=&gt;45.0, "hour"=&gt;16.0, "date"=&gt;5.0, "month"=&gt;9.0, "year"=&gt;2011.0, "count"=&gt;518.0} ] } </code></pre> <p><em>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</em> </p> <p>So basically each array of hashes would be grouped by the value of its <code>type</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload