I have a array of hashes like this.
[
{"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}
]
And I want this be like this.
[
{"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"}
]},
]
Hash should merge according to the equal event_id values and there event can have many packages.
I want to know the easiest way to do this. Though I was able to do with many if and else s.
{which denotes a Hash literal, but it appears that you have combinations of arrays and hashes. Please edit your question to show the actual data you have. (Hint:p my_datawill print the.inspectresult.)