以不同方式合并三个哈希数组

Merge three arrays of hashes in different way

我是 Ruby 的新手,正在尝试构建一个会议应用程序。我有三个包含哈希的数组:

具体化为:

meetings = [
 {:id=>"1", :peoples=>[]}
 {:id=>"2", :peoples=>[]}
 {:id=>"3", :peoples=>[]}
]

invited_peoples = [
 {:id=>"1", :peoples=>['Tom', 'Henry', 'Georges', 'Nicolas']}
 {:id=>"2", :peoples=>['Arthur', 'Carl']}
]

absent_peoples = [
 {:id=>"1", :peoples=>['Henry', 'Georges']}
]

我想举行:会议 + invited_peoples - absent_peoples 喜欢

meetings_with_participants = [
 {:id=>"1", :peoples=>['Tom', 'Nicolas']}
 {:id=>"2", :peoples=>['Arthur', 'Carl']}
 {:id=>"3", :peoples=>[]}
]

我正在寻找可读的解决方案,但找不到任何人...

对不起我的英语,提前谢谢你, 尼古拉斯

创建一个简单的散列

h = meetings.each_with_object({}) { |g,h| h[g[:id]] = g[:peoples] }
  #=> {"1"=>[], "2"=>[], "3"=>[]}

添加受邀者

invited_peoples.each { |g| h[g[:id]] += g[:peoples] }

现在

h #=> {"1"=>["Tom", "Henry", "Georges", "Nicolas"],
  #    "2"=>["Arthur", "Carl"], "3"=>[]} 

删除被拒绝者

absent_peoples.each { |g| h[g[:id]] -= g[:peoples] }          

现在

h #=> {"1"=>["Tom", "Nicolas"], "2"=>["Arthur", "Carl"],
  #    "3"=>[]} 

将散列转换为散列数组

h.map { |k,v| { :id=> k, :peoples=> v } }
  #=> [{:id=>"1", :peoples=>["Tom", "Nicolas"]},
  #    {:id=>"2", :peoples=>["Arthur", "Carl"]},
  #    {:id=>"3", :peoples=>[]}] 

我最初创建了一个散列,只有在处理完受邀者和拒绝者之后,我才将其转换为散列数组。这样做可以加快 :id 添加和删除人员的查找速度。因此,如果 n = meetings.size,这些计算的计算复杂度接近 O(n),"close to" 因为哈希键查找的计算复杂度接近 O(1)(即时间需要找到一个键,它的值几乎是恒定的,无论散列的大小如何)。相比之下,对于 meetings 的每个元素,在 invited_peoplesabsent_peoples 中搜索 :id 值的方法的计算复杂度为 O(n 2).

定义一个通过id查找对象的方法

def find_by_id array_of_hash, id
  array_of_hash.find {|x| x[:id] == id} || {peoples: []}
end

用map转一个新数组,在map块里面就用你的逻辑meetings + invited_peoples - absent_peoples like

result = meetings.map do |item|
  id = item[:id]
  {id: id, peoples: item[:peoples] + find_by_id(invited_peoples, id)[:peoples] - find_by_id(absent_peoples, id)[:peoples]}
end

结果:

=> [{:id=>"1", :peoples=>["Tom", "Nicolas"]}, {:id=>"2", :peoples=>["Arthur", "Carl"]}, {:id=>"3", :peoples=>[]}]