I'm trying to get this object, passed via AJAX:
Parameters: {"status"=>{"1"=>["14", "1"], "2"=>["7", "8", "12", "13"]}}
into something like:
14 -> 1
1 -> 1
7 -> 2
over which I can iterate.
What's the most elegant way of achieving this?
flat_inverse = {}
parameters["status"].each { |key, values| values.each { |v| flat_inverse[v] = key } }
flat_inverse
# {"14"=>"1", "1"=>"1", "7"=>"2", "8"=>"2", "12"=>"2", "13"=>"2"}
#or more functional
Hash[*parameters["status"].map { |k, vs| vs.zip([k] * v.length) }.flatten]
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