根据多个其他数组检查数组中对象属性的最快方法?

问题描述

我有一个对象数组,“客户”,它们有很多属性包括 idassociated_road

associated_road 是“道路”内对象的 ID,如下所述。

我有一个对象数组“old_customer_list”,它具有与“customers”相同的属性字段。

注意:来自“old_customer_list”的一些客户将存在于具有相同 ID 的“customers”中。

我还有一个对象数组 'roads',它有自己的属性,与上面的不同,我将使用的一个例子是 road_type

我想做的是:

  1. 使用 associated_road 遍历“old_customers”以检查“roads”,为客户找到关联的 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

这已经很慢了,所以如果有更清晰的方法来做到这一点,或者如果这部分完全可以跳过,我会全力以赴。

  1. 从这里开始,我想检查“customers”数组,主要目的是更新“关联道路”属性 IF:

old_customers_with_road 包含匹配的 ID(来自 old_customer_list 的 cust.id)并且“roads”数组具有匹配的 road_type(匹配来自“old_customers_withroads”数组的 matched_road[0].road_type ).

我不确定如何最好地做到这一点,避免使用 any? eachselect 对多个数组进行嵌套迭代,因为这些数组可以包含数千个对象。

感谢任何指点!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)