Get all keys in hash with same value

前端 未结 3 492
星月不相逢
星月不相逢 2021-01-07 12:32

I am trying to get all the keys with the same value from a hash and put them into an array as separate entries. I have this line of code but it sends everything in as a sing

3条回答
  •  耶瑟儿~
    2021-01-07 13:27

    Maybe this?

    h.select {|k, v| v == val}.keys.each {|k| @highest_wf_words << [k]}
    

    Or this:

    @highest_wf_words.concat(h.select {|k, v| v == val}.keys.map {|k| [k]})
    

提交回复
热议问题