简体   繁体   中英

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. Is there anything where I can do something like this: 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM