これは私がRubyで扱っているオブジェクトです。これは日付順にソートされた一連のイベントですが、オブジェクト内に同じ event_type を持つ複数のイベントが隣り合っている場合、オブジェクトを 1 レベル深くネストしたいと考えています。
@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"}
]
私はそれをこれに変えようとしています:
@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"}
]
入れ子になったオブジェクトを日付順に並べ替えるには、オブジェクト全体が必要ですが、2 つ以上のイベントがクラスター化されている場合は、サンプル出力に見られるように、それらを配列に変換する必要があります。単純な問題のように思えますが、理解できないようです。