ruby – 如何根据哈希值合并两个哈希数组?
发布时间:2020-12-17 03:44:53 所属栏目:百科 来源:网络整理
导读:我怎么转这个: first_array = [ {:count=nil,:date="Jan 31"},{:count=nil,:date="Feb 01"},:date="Feb 02"},:date="Feb 03"},:date="Feb 04"},:date="Feb 05"}]second_array = [ {:count=12,{:count=2,:date="Feb 05"}] 进入: result = [ {:count=nil,{:c
我怎么转这个:
first_array = [ {:count=>nil,:date=>"Jan 31"},{:count=>nil,:date=>"Feb 01"},:date=>"Feb 02"},:date=>"Feb 03"},:date=>"Feb 04"},:date=>"Feb 05"} ] second_array = [ {:count=>12,{:count=>2,:date=>"Feb 05"} ] 进入: result = [ {:count=>nil,{:count=>12,:date=>"Feb 05"} ] 我在SO上发现了类似的问题,但没有一个像这个一样简单.可能有一个我应该使用的方法/块组合我不知道. 解决方法result_array = first_array.map do |first_hash| second_array.each do |second_hash| if first_hash[:date] == second_hash[:date] first_hash[:count] = second_hash[:count] break end end first_hash end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |