如何循环哈希数组并删除密钥具有特定值的哈希?

时间:2016-05-27 20:39:16

标签: arrays ruby hash

我试图遍历包含一组键和值的哈希数组,在这个循环中我想检查是否有任何键(或一组特定的键,无论最简单的是什么)都有一个一定的价值。

这是我到目前为止所做的,但它不起作用,因为包含值为 dollar 的键的哈希仍然存在于数组中:

remove_currency = [{a: 'fruit', b: 'dollar'}, {a: 'fruit', b: 'yen'}]
currency = 'dollar'
remove_currency.delete_if { |_, v| v == currency }

希望我让自己足够清楚!

1 个答案:

答案 0 :(得分:4)

things = [{foo: 3, bar: 42}, {baz: 5, quiz: 3.14}]
things.reject { |thing| thing.values.include? 42 }
  # => [{:baz=>5, :quiz=>3.14}]