我有这个哈希数组:
results = [ {"day"=>"2012-08-15","name"=>"John","calls"=>"5"},{"day"=>"2012-08-15","name"=>"Bill","calls"=>"8"},{"day"=>"2012-08-16","calls"=>"11"},]
如何搜索结果以查找Bill在15日拨打了多少电话?
在阅读“Ruby easy search for key-value pair in an array of hashes”的答案后,我认为可能涉及扩展以下查找声明:
results.find { |h| h['day'] == '2012-08-15' }['calls']
解决方法
你走在正确的轨道上!
results.find {|i| i["day"] == "2012-08-15" and i["name"] == "Bill"}["calls"] # => "8"