ruby—检查数组中对象属性与其他多个数组的最快方法?

yftpprvb  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(248)

我有一个对象数组,“客户”有很多属性,包括 id
associated_road associated_road 是“道路”内对象的id,如下所示。
我还有另一个对象数组“old_customer_list”,它的属性字段与“customers”相同。
注意:“旧客户”列表中的一些客户将存在于具有相同id的“客户”中。
我还有一个对象数组“roads”,它有自己的属性,与上面不同,我将使用一个示例 road_type 我想做的是:
使用 associated_road 检查“道路”以查找相关的 road_type 为了顾客。

old_customers_with_road = []

old_customer_list.each do |cust|
  matched_road = roads.select { |road| road.id == cust.associated_road }
  old_customers_with_road << [cust.id, cust.associated_road, matched_road[0].road_type]
end

这已经很慢了,所以如果有一个更清晰的方法来做这件事,或者如果这部分完全可以跳过,我洗耳恭听。
从这一点上,我想检查“customers”数组,主要目的是在以下情况下更新“associated_road”属性: old_customers_with_road 包含匹配的id( cust.id “roads”数组具有匹配的 road_type (匹配 matched_road[0].road_type 来自“老路客户”阵列)。
我不确定如何最好地做到这一点,避免使用
any? eachselect 因为这些数组中可以包含数千个对象。
感谢任何指点!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题