简体   繁体   中英

How do I get all the values from a hash that are in an array?

I have the following hash wbs_items :

wbs_items = {"Architecture"=>"architecture", "Auditing"=>"auditing", "Consulting"=>"consulting", "Delivery"=>"delivery", "Development"=>"development", "Engineering"=>"engineering", "Environment / IT"=>"environment", "Graphic Design"=>"graphic_design", "Management"=>"management", "Requirements"=>"requirements", "Research"=>"research", "Support"=>"support", "System Design"=>"system_design", "Test & Eval"=>"test_and_evaluation", "Writing"=>"writing"}

And the following array disciplines :

disciplines = ["architecture", "auditing", "consulting", "development", "engineering", "environment", "graphic_design", "management", "system_design", "test_and_evaluation", "writing"]

I want to get only the keys and values from the hash that have values from the array. How would I do that?

尝试这个:

wbs_items.values & disciplines

关于什么 :

wbs_items.select { |k,v| disciplines.include?(v) }
inverted = wbs_items.invert
disciplines.to_enum.with_object({}){|k, h| h[inverted[k]] = k}

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