bork bork - 2 years ago 67
Ruby Question

How to loop over array of hashes and remove a hash where a key has a specific value?

I'm trying to loop over an array of hashes containing a set of keys and values, in this loop I want to check whether any key (or a set of specific keys, whatever that is most simple) has a certain value.

This is what I've got so far, but it doesn't work as the hashes containing a key with the value dollar is still present within the array:

currency = 'dollar'
remove_currency.delete_if { |_, v| v == currency }

Hope I made myself clear enough!

ndn ndn
Answer Source
things = [{foo: 3, bar: 42}, {baz: 5, quiz: 3.14}]
things.reject { |thing| thing.values.include? 42 }
  # => [{:baz=>5, :quiz=>3.14}]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download