Ruby group_by 对象?
我有一个数组或不同的对象,我想按对象分组。例如,
=> [#<Graphic id: 3...">, #<Collection id: 1....">, #<Category id:...">, #<Volume id: 15...">]
all.size
=> 4
我尝试过
all.group_by(Object)
,但没有用...有关如何对一个数组中的对象进行分组的任何想法吗?
I have an array or different objects and I want to group by objects. For example
=> [#<Graphic id: 3...">, #<Collection id: 1....">, #<Category id:...">, #<Volume id: 15...">]
all.size
=> 4
I tried
all.group_by(Object)
but that didn't work...any ideas on how to groupby objects in one array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想做这样的事情吗?
它将按类名对数组中的对象进行分组,
编辑注释
Key 是分组元素,obj 是键的集合,因此这将循环分组中的每个组并列出该组中的对象
您也可以在其中排序分组也很容易
Are you looking to do something like this?
Which will group the objects in array by their class name
EDIT for comment
Key is the grouping element and obj is the collection for the key, so this would loop through each group in the grouping and list the objects within that group
Also you could sort within the groupings pretty easily too