I have an array consisting of hashes in the following form:
[
{:user=>"mike" etc},
{:user=>"mike" etc},
{:user=>"peter" etc},
{:user=>"joe" etc}
]
Are there any convenient ways to split the group according to the value of user key? The final result should be something like this:
[
[{:user=>"mike" etc}, {:user=>"mike" etc}],
[{:user=>"peter" etc}],
[{:user=>"joe" etc}]
]
Use group_by
.
array.group_by{|h| h[:user]}.values
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With