[英]Convert array to hash with custom format
Can somebody help me to convert the following array to a hash with the following format?有人可以帮我将以下数组转换为具有以下格式的 hash 吗?
Array大批
[["0", {"checkbox_2"=>"on"}], ["2", {"checkbox_1"=>"on"}]]
Hash Hash
search=>{"checkbox_2"=>"on", "checkbox_1"=>"on"}
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"}}
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"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.