考虑单个散列键值合并散列数组

时间:2013-03-24 08:22:49

标签: ruby

我有一系列像这样的哈希。

[
 {"package_details"=>{"name"=>"Package3", "price"=>3000.0, "id"=>"281"},"event_id"=>336},
 {"package_details"=>{"name"=>"2000/-", "price"=>2000.0, "id"=>"280"}, "event_id"=>337}, 
 {"package_details"=>{"name"=>"Package1", "price"=>1000.0, "id"=>"282"},"event_id"=>337},
 {"package_details"=>{"name"=>"Package2", "price"=>2000.0, "id"=>"283"},"event_id"=>337}
]

我希望这就像这样。

[
  {"event_id"=>336, "package_details"=>[
    {"name"=>"Package3", "price"=>3000.0, "id"=>"281"}
  ]},
  {"event_id"=>337, "package_details"=>[
    {"name"=>"2000/-", "price"=>2000.0, "id"=>"280"},
    {"name"=>"Package1", "price"=>1000.0, "id"=>"282"},
    {"name"=>"Package2", "price"=>2000.0, "id"=>"283"}
  ]},
]

哈希应该根据相等的event_id值进行合并,并且事件可以有很多包。

我想知道最简单的方法。虽然我能用许多if和else s。

2 个答案:

答案 0 :(得分:5)

为您提供所需功能结果的最简单转换是使用Enumerable#group_by

details = [
 {"package_details"=>{"name"=>"Package3","price"=>3000.0,"id"=>"281"},"event_id"=>336},
 {"package_details"=>{"name"=>"2000/-","price"=>2000.0,"id"=>"280"},"event_id"=>337}, 
 {"package_details"=>{"name"=>"Package1","price"=>1000.0,"id"=>"282"},"event_id"=>337},
 {"package_details"=>{"name"=>"Package2","price"=>2000.0,"id"=>"283"},"event_id"=>337}
]

grouped = details.group_by{ |d| d["event_id"] }
#=> {
#=>   336=>[
#=>     {"package_details"=>{"name"=>"Package3", "price"=>3000.0, "id"=>"281"}, "event_id"=>336}],
#=>   337=>[
#=>     {"package_details"=>{"name"=>"2000/-", "price"=>2000.0, "id"=>"280"}, "event_id"=>337},
#=>     {"package_details"=>{"name"=>"Package1", "price"=>1000.0, "id"=>"282"}, "event_id"=>337},
#=>     {"package_details"=>{"name"=>"Package2", "price"=>2000.0, "id"=>"283"}, "event_id"=>337}]
#=> }

有了这个你就可以做到:

grouped.each do |event_id, packages|
  # event_id is the actual value, like 336
  # packages is an array of hashes from your original
end

如果您确实需要您所描述的格式,那么您可以这样做:

transformed = details.group_by{ |d| d["event_id"] }.map do |event_id, hashes|
  {
    "event_id"=>event_id,
    "package_details"=>hashes.map{ |h| h["package_details"] }
  }
end    
#=> [
#=>   {"event_id"=>336, "package_details"=>[
#=>     {"name"=>"Package3", "price"=>3000.0, "id"=>"281"}
#=>   ]},
#=>   {"event_id"=>337, "package_details"=>[
#=>     {"name"=>"2000/-", "price"=>2000.0, "id"=>"280"},
#=>     {"name"=>"Package1", "price"=>1000.0, "id"=>"282"},
#=>     {"name"=>"Package2", "price"=>2000.0, "id"=>"283"}
#=>   ]}
#=> ]

以上内容贯穿分组,并将每个键/值对转换(“映射”)为自定义哈希,其内容已根据您的需求进行了分析。

答案 1 :(得分:0)

最后我明白了。对于上面的输入哈希,我设法得到这样的。

[{"packages"=>[{"name"=>"Package3", "price"=>3000.0, "id"=>"281"}], "event_id"=>336}, {"packages"=>[{"name"=>"2000/-", "price"=>2000.0, "id"=>"280"}, {"name"=>"Package1", "price"=>1000.0, "id"=>"282"}, {"name"=>"Package2", "price"=>2000.0, "id"=>"283"}], "event_id"=>337}

使用这些方法。这不是最准确的方法。但它为我做了诀窍。

event_id_array = Array.new
events_with_packages = Array.new
events.each.each do |event|
event_id_array.push(event.id)
end
event_id_array.uniq!.each_with_index do |e_id, i|
  events_with_packages[i]  = Hash.new
  events_with_packages[i]["event_id"] = e_id
  events_with_packages[i]["packages"] = Array.new
    events.each do |event|
    tmp_event = Hash.new
    if(event.id==e_id)
      tmp_event["id"] = event[:ep_id]
      tmp_event["price"] = event[:price]
      tmp_event["name"] = event[:ep_name]
      events_with_packages[i]["packages"].push(tmp_event)
    end
  end
end
events_with_packages # the answer