[英]Finding unique hash value within array of hashes (Ruby)
a[0] = {:id => '1234', :value => '37'}
a[1] = {:id => '4321', :value => '50'}
a[2] = {:id => '1122', :value => '50'}
From here I want to be able to check to see if a hash exists with :id => '4321' without having to loop through the array manually. 从这里,我希望能够检查:id =>'4321'是否存在哈希,而不必手动遍历数组。 Is there anything where I can do something like this: a.exists?(:id => '4321') ?
有什么我可以做的事情:a.exists?(:id =>'4321')吗? I've tried a few things but can't seem to figure it out.
我已经尝试了一些方法,但是似乎无法弄清楚。 Thanks!
谢谢!
How about: 怎么样:
a.any? {|x| x[:id] == '4321' }
That will return true
if the block returns true
. 这将返回
true
如果块返回true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.