我有以下内容:
array_of_hashes = [{:a=>10, :b=>20}, {:a=>11, :b=>21}, {:a=>13, :b=>23}]
Run Code Online (Sandbox Code Playgroud)
如何查找是否:a=>11存在array_of_hashes
array_of_hashes.include? 似乎不起作用
Dig*_*oss 18
你确实在OQ中要求一个布尔结果,但是如果你真的想要哈希元素本身呢:
array_of_hashes.detect { |h| h[:a] == 11 }
Run Code Online (Sandbox Code Playgroud)
如果您希望结果非常快,您可以对原始对象进行分组,然后通过单个哈希查找获得结果:
t = array_of_hashes.group_by { |x| x[:a] }
t[11]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20573 次 |
| 最近记录: |