简体   繁体   English

来自Rails 4中具有匹配数组值的哈希的所有键

[英]All keys from hash with matching array values in Rails 4

I have a hash such as: 我有一个哈希,例如:

CHAR_MAP = {
  :x => %w[something]
  :y => %w[something2 something]
  :z => %w[something3 something1]
  :r => %w[something something3]
  ...
  ...
}

And I have another array 我还有另一个数组

x = ['something', 'something3']

Now for the hash CHAR_MAP I want a list of keys for which all the values in it's corresponding value array are a part of the array x 现在,对于散列CHAR_MAP我想要一个键列表,其对应的value array中的所有值都属于数组x的一部分

So for array x we should get the keys x and r as output 因此,对于array x我们应该获取键xr作为输出

@recommendation_factors.each do |rf|
  @rec_list = CHAR_MAP.detect{|h| h == rf.recommendation_factor}
end

I tried the above but it keeps on returning an empty array. 我尝试了上面,但它继续返回一个空数组。 :/ :/

试试这个

CHAR_MAP.keys.select { |k| (CHAR_MAP[k] - x).empty? }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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