How do I find keys in my hash to map to elements that match a condition?

后端 未结 3 1611
北荒
北荒 2021-01-27 20:58

I have a hash that maps integers to arrays. For example

{1 => [\"abc\"], 2 => [\"ccc\", \"ffffd\"]}

How do I get all the keys from my hash

相关标签:
3条回答
  • 2021-01-27 21:37

    Anything like this?

    hash.each_key.select { |key| hash[key].count >= 2 }
    
    0 讨论(0)
  • 2021-01-27 21:42

    One more possible solution :)

    {1 => ["abc"], 2 => ["ccc", "ffffd"]}.map { |k, v| k if v.size > 1 }.compact
    # => [2]
    
    0 讨论(0)
  • 2021-01-27 21:50
    {1 => ["abc"], 2 => ["ccc", "ffffd"]}.select{|_, a| a.length > 1}.keys
    # => [2]
    
    0 讨论(0)
提交回复
热议问题