Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个由以下形式的哈希组成的数组:
[ {:user=>"mike" etc}, {:user=>"mike" etc}, {:user=>"peter" etc}, {:user=>"joe" etc} ]
有没有方便的方法根据用户键的值来拆分组?最终结果应该是这样的:
[ [{:user=>"mike" etc}, {:user=>"mike" etc}], [{:user=>"peter" etc}], [{:user=>"joe" etc}] ]
使用group_by.
group_by
array.group_by{|h| h[:user]}.values