在具有多个条件的哈希数组中进行 Ruby 搜索



我正在尝试查询具有 2 种不同条件的哈希数组,但这不起作用:

array_list = [
{type: 'sale', currency: 'CAD', price: '123'},
{type: 'purchase', currency: 'CAD', price: '321'}
]
if array_list.select { |pt|
pt[:type] == 'sale', pt[:currency] == 'CAD'
}.present?
end

有什么建议吗? 谢谢

你也需要使用&&而不是使用Array#select,present?可以使用Array#any?

array_list = [
{type: 'sale', currency: 'CAD', price: '123'},
{type: 'purchase', currency: 'CAD', price: '321'}
]
if array_list.any? { |pt| pt[:type] == 'sale' && pt[:currency] == 'CAD'}
# your logic
end

最新更新