我有一个conditions
哈希:
{"age" => "52", "First_Name" => "Bat"}
我有一系列person
哈希:
[{"id"=>"1", "First_Name"=>"John", "Last_Name"=>"Doe", "Age"=>"27"}, {"id"=>"2", "First_Name"=>"Jane", "Last_Name"=>"Doe", "Age"=>"26"}, {"id"=>"3", "First_Name"=>"Bat", "Last_Name"=>"Man", "Age"=>"52"}, {"id"=>"4", "First_Name"=>"Bat", "Last_Name"=>"Woman", "Age"=>"51"}]
如何获取匹配person
的所有conditions
哈希值? (在这种情况下,它应该只返回“蝙蝠侠”哈希。)
答案 0 :(得分:10)
如果您想所有匹配conditions
的人
people.select do |person|
conditions.all? { |k,v| person[k] == v }
end
或者,如果你确定只会有一个(感谢@tokland):
people.detect do |person|
conditions.all? { |k,v| person[k] == v }
end