0

誰かが次の配列を次の形式のハッシュに変換するのを手伝ってもらえますか?

配列

[["0", {"checkbox_2"=>"on"}], ["2", {"checkbox_1"=>"on"}]]

ハッシュ

search=>{"checkbox_2"=>"on", "checkbox_1"=>"on"}
4

2 に答える 2

2
xs = [["0", {"checkbox_2"=>"on"}], ["2", {"checkbox_1"=>"on"}]]
search = {:search => xs.map { |n, h| h }.inject(:merge)}
# {:search=>{"checkbox_2"=>"on", "checkbox_1"=>"on"}}
于 2012-04-09T10:08:47.750 に答える
1
arr = [["0", {"checkbox_2"=>"on"}], ["2", {"checkbox_1"=>"on"}]] 
hash = Hash[arr.flatten.select{|e| e.is_a? Hash}.collect{|e| e.to_a.flatten}]
=> {"checkbox_2"=>"on", "checkbox_1"=>"on"}
于 2012-04-09T09:44:24.550 に答える