[英]How do I group objects into nested objects by a value in the object?
Here's an object I'm working with in Ruby. 这是我在Ruby中使用的对象。 It's a bunch of events sorted by date, but I want to nest the object one level deeper when there are two or more with the same event_type next to each other in the object. 这是一堆按日期排序的事件,但是当对象中有两个或多个具有相同event_type的对象彼此相邻时,我想将对象嵌套得更深。
@events = [
{ :id => 1, :event_type => "B", :created_at => "2013-09-30 14:44:24 UTC"},
{ :id => 2, :event_type => "A", :created_at => "2013-09-29 14:44:24 UTC"},
{ :id => 3, :event_type => "A", :created_at => "2013-09-28 14:44:24 UTC"},
{ :id => 4, :event_type => "C", :created_at => "2013-09-27 14:44:24 UTC"},
{ :id => 5, :event_type => "C", :created_at => "2013-09-26 14:44:24 UTC"},
{ :id => 6, :event_type => "A", :created_at => "2013-09-25 14:44:24 UTC"}
]
I'm looking to turn it into this: 我想把它变成这样:
@events = [
{ :id => 1, :event_type => "B", :created_at => "2013-09-30 14:44:24 UTC"},
{ :grouped_events => [
{ :id => 2, :event_type => "A", :created_at => "2013-09-29 14:44:24 UTC"},
{ :id => 3, :event_type => "A", :created_at => "2013-09-28 14:44:24 UTC"}
]
},
{ :grouped_events => [
{ :id => 4, :event_type => "C", :created_at => "2013-09-27 14:44:24 UTC"},
{ :id => 5, :event_type => "C", :created_at => "2013-09-26 14:44:24 UTC"}
]
},
{ :id => 6, :event_type => "A", :created_at => "2013-09-25 14:44:24 UTC"}
]
I need the overall object to keep the nested objects sorted by date, but if there are 2 or more events clustered together, I want them turned into an array, as seen by the sample output. 我需要总体对象来保持嵌套对象按日期排序,但是如果有2个或更多事件聚集在一起,我希望它们变成一个数组,如示例输出所示。 It seems like a simple problem, but I can't seem to figure it out. 这似乎是一个简单的问题,但我似乎无法弄清楚。
To keep order use chunk method of Enumerable
: 要保持订单,请使用Enumerable
块方法:
@events = [
{ :id => 1, :event_type => "B", :created_at => "2013-09-30 14:44:24 UTC"},
{ :id => 1, :event_type => "A", :created_at => "2013-09-29 14:44:24 UTC"},
{ :id => 1, :event_type => "A", :created_at => "2013-09-28 14:44:24 UTC"},
{ :id => 1, :event_type => "C", :created_at => "2013-09-27 14:44:24 UTC"},
{ :id => 1, :event_type => "C", :created_at => "2013-09-26 14:44:24 UTC"},
{ :id => 1, :event_type => "A", :created_at => "2013-09-25 14:44:24 UTC"}
]
p @events.chunk{|el| el[:event_type]}.map{|gr| gr[1]}
Output: 输出:
#=> [[{:id=>1, :event_type=>"B", :created_at=>"2013-09-30 14:44:24 UTC"}],
#=> [{:id=>1, :event_type=>"A", :created_at=>"2013-09-29 14:44:24 UTC"},
#=> {:id=>1, :event_type=>"A", :created_at=>"2013-09-28 14:44:24 UTC"}],
#=> [{:id=>1, :event_type=>"C", :created_at=>"2013-09-27 14:44:24 UTC"},
#=> {:id=>1, :event_type=>"C", :created_at=>"2013-09-26 14:44:24 UTC"}],
#=> [{:id=>1, :event_type=>"A", :created_at=>"2013-09-25 14:44:24 UTC"}]]
Or, if you like to have arrays only when there 2 or more elements try this: 或者,如果您只想在有2个或更多元素时才拥有数组,请尝试以下操作:
p @events
.chunk{|el| el[:event_type]}
.flat_map{|gr| gr[1].length == 1 ? gr[1] : [gr[1]]}
Output will be 输出将是
#=> [
#=> {:id=>1, :event_type=>"B", :created_at=>"2013-09-30 14:44:24 UTC"},
#=> [{:id=>1, :event_type=>"A", :created_at=>"2013-09-29 14:44:24 UTC"},
#=> {:id=>1, :event_type=>"A", :created_at=>"2013-09-28 14:44:24 UTC"}],
#=> [{:id=>1, :event_type=>"C", :created_at=>"2013-09-27 14:44:24 UTC"},
#=> {:id=>1, :event_type=>"C", :created_at=>"2013-09-26 14:44:24 UTC"}],
#=> {:id=>1, :event_type=>"A", :created_at=>"2013-09-25 14:44:24 UTC"}
#=> ]
Update 更新
You change output format on the fly :) 您可以随时更改输出格式:)
For your last variant try this: 对于您的最后一个变体,请尝试以下操作:
p @events
.chunk{|el| el[:event_type]}
.flat_map{|gr|
gr[1].length == 1 ? gr[1] : {:grouped_events => gr[1]}
}
What about 关于什么
p @events.group_by{|item| item[:event_type]}.values #=>
# [[{:id=>1, :event_type=>"B", :created_at=>"2013-09-30 14:44:24 UTC"}],
# [{:id=>1, :event_type=>"A", :created_at=>"2013-09-29 14:44:24 UTC"},
# {:id=>1, :event_type=>"A", :created_at=>"2013-09-28 14:44:24 UTC"},
# {:id=>1, :event_type=>"A", :created_at=>"2013-09-25 14:44:24 UTC"}],
# [{:id=>1, :event_type=>"C", :created_at=>"2013-09-27 14:44:24 UTC"},
# {:id=>1, :event_type=>"C", :created_at=>"2013-09-26 14:44:24 UTC"}]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.