这是我的哈希:
react-native run-ios
您可以从中看到,每个哈希表中的密钥{"Specialty"=>"Other (note in description);Medical;Dental;Vision", "Value"=>"https://www.example.com/ca"}
{"Value"=>"P.O. BOX 60007 LOS ANGELES, CA 90060"}
{"Specialty"=>"Pharmacy;Medical", "Value"=>"800-824-0898"}
{"Specialty"=>"Urgent Care;Medical", "Value"=>"800-700-9186"}
{"Specialty"=>"Urgent Care;Emergency Care;Medical;Chiropractor;Acupuncture", "Value"=>"800-677-6669"}
{"Specialty"=>"Pharmacy;Medical", "Value"=>"https://www.example.com"}
{"Specialty"=>"Claims", "Value"=>"https://www.example.com/consumer/claims/claimsoverview"}
{"Specialty"=>"Urgent Care;Emergency Care;Medical;Chiropractor;Acupuncture", "Value"=>"https://example/example/apps/services/www/ABCBS/mobilewebapp/default/index.html#/providers/search"}
{""Specialty"=>"Claims;Customer Service;Medical", "Value"=>"800-888-8288"}
都有value
个不同,我想只打印一个特定值,即values
我尝试用P.O. BOX 60007 LOS ANGELES, CA 90060
打印哈希值,然后打印值的所有内容。我尝试使用hash["value"]
函数将它们加载到array[]
中以打印我想要的值,但是一个数组加载了所有这些值,或者我可能正在加载数组错误。
to_s
有人可以帮助我如何打印特定的哈希值,尽管名为array = []
array = medical.split("\n")
text = array.to_s
text1 = array.index{|s| s.include?("P.O.")}
puts text1
答案 0 :(得分:2)
如果具体是hash
的模式,我可以理解的唯一性是所有哈希都有多个键,除了你正在寻找的那个......把它当作一个哈希数组
hashes.each{ |hash| break hash['Value'] if hash.keys == ['Value'] }
<强>更新强>
从评论中,您可以直接检查并打印所需的值,而不是单独打印哈希值。
results1.map{ |result1| (value = result1['Value']).include?('P.O. BOX') ? value : nil }.compact
它将返回具有子串P.O. BOX
答案 1 :(得分:1)
假设你有一系列哈希,你想搜索'P.O.'字符串然后你可以这样做:
arr.select { |hash| hash["Value"][/P\.O\./] }.flat_map(&:values)
#=> ["P.O. BOX 60007 LOS ANGELES, CA 90060"]
答案 2 :(得分:1)
array_hashes = [
{"Specialty"=>"Other (note in description);Medical;Dental;Vision", "Value"=>"https://www.example.com/ca"},
{"Value"=>"P.O. BOX 60007 LOS ANGELES, CA 90060"},
{"Specialty"=>"Pharmacy;Medical", "Value"=>"800-824-0898"},
{"Specialty"=>"Urgent Care;Medical", "Value"=>"800-700-9186"},
{"Specialty"=>"Urgent Care;Emergency Care;Medical;Chiropractor;Acupuncture", "Value"=>"800-677-6669"},
{"Specialty"=>"Pharmacy;Medical", "Value"=>"https://www.example.com"},
{"Specialty"=>"Claims", "Value"=>"https://www.example.com/consumer/claims/claimsoverview"},
{"Specialty"=>"Urgent Care;Emergency Care;Medical;Chiropractor;Acupuncture", "Value"=>"https://example/example/apps/services/www/ABCBS/mobilewebapp/default/index.html#/providers/search"},
{"Specialty"=>"Claims;Customer Service;Medical", "Value" => "800-888-8288"}]
> array_hashes.select{|h| h["Value"].include?("P.O.")}.flat_map(&:values).first
#=> "P.O. BOX 60007 LOS ANGELES, CA 90060"