简体   繁体   中英

Return a unique array of values from array of hashes

I have an array @contacts of hashes, each of which has the following keys:

:full_name
:city
:state
:email

How would I iterate through the array to return the :city values uniquely? Below is my best attempt

@contacts.uniq {|hash| hash[:city]}
@contacts.map { |k| k[:city] }.uniq

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