Using Ruby I need to convert this...
{"30"=>["Morgan", ["lib1", "lib2"]],
"31"=>["Morgan", ["lib9", "lib2", "lib3"]],
"32"=>["Gary", ["lib1", "lib2"]],
"33"=>["Morgan", ["lib1"]],
"34"=>["Morgan", []],
"35"=>["Morgan", []],
"36"=>["Morgan", ["lib3", "lib2"]],
"37"=>["jim", ["lib1"]]}
into this...
{"Morgan"=>[30,31,33,34,35,36],
"Gary"=>[32], []=>[34,35],
"jim"=>[37]}
Any ideas?
input.each_with_object(Hash.new{|h,k|h[k]=[]}) do |(k,v),res|
(v.last.empty? ? res[[]] : res[v.first]) << k.to_i
end
=> {"Morgan"=>[30, 31, 33, 36], "Gary"=>[32], []=>[34, 35], "jim"=>[37]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With