简体   繁体   English

如何通过对象中的值将对象分组为嵌套对象?

[英]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.

相关问题 如何根据对象内的值对 REST API 返回的对象进行分组? - How do I group objects returned by a REST API based on a value inside that object? 如何将这些对象分组为哈希? - How do I group these objects into a hash? 在进入Rails中的下一个对象之前,如何在一个对象及其嵌套对象之间循环? - How do I cycle through an object and it's nested objects before moving on to the next object in Rails? 如何获取对象的子对象的所有子对象? - How do I get all the children objects of the children objects of an object? 您如何在Emberjs中访问嵌套对象? - How do you access nested objects in Emberjs? 如何优雅地检查对象和关联对象的存在? - How do I elegantly check for presence of both the object and associated objects? 如何在 Rails 上使用 Ruby 将对象数组作为值包含在表中? - How do I include an array of objects as a value in a table with Ruby on Rails? 在Rails中,如何使用accepts_nested_attributes_for创建嵌套对象? - In Rails, how do I create nested objects using accepts_nested_attributes_for? 如何按字段创建组对象 - How can I create group objects by a field 如何基于列值分离对象并检索和检索每个组的第一个对象? - How to separate objects based on column value and retrieve and retrieve the first object of each group?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM