ruby-on-rails - 具有多个条件的哈希数组中的 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
推荐阅读
- python - python中自定义类对象的参数传递:按引用调用还是按值调用评估?
- java - Java array.sort(arr) 输出 0 而不是 arr
- c# - 同时运行异步和同步方法
- python - 在字典中查找相似的列表值
- php - 如何完成在文件夹中显示文件的搜索表单的代码?
- javascript - 来自 node-js mysql 连接的错误未保存到数组
- apache - Apache说配置了虚拟主机,但我找不到
- java - 通用方法的 JUnit 测试用例
- java - 用完短值来检测 LIBGDX 中的冲突
- ruby - 使用 Azure 数据工厂 v2 在本地服务器上调用 Ruby 脚本或可执行文件?