ruby remove item from array based on another array -


i have 2 data,

{:date=>"2015-09-23", :time=>["13:30"]} {:date=>"2015-09-23", :time=>["12:00"]} ...more 

and try remove from

{:date=>"2015-09-21", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]} {:date=>"2015-09-22", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]} {:date=>"2015-09-23", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]} {:date=>"2015-09-24", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]} 

and output hope can get

{:date=>"2015-09-21", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]} {:date=>"2015-09-22", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]} {:date=>"2015-09-23", :time=>["12:30", "13:00", "14:00", "14:30"]} {:date=>"2015-09-24", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]} 

i try

removeary.each |b|   if b[:date] == "2015-09-23"     puts b[:time].to_s.delete("13:30")   end end 

do have better idea ?

ok, here's do:

require 'set'  to_delete = [   {:date=>"2015-09-23", :time=>["13:30"]},   {:date=>"2015-09-23", :time=>["12:00"]} ]  data = [   {:date=>"2015-09-21", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]},   {:date=>"2015-09-22", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]},   {:date=>"2015-09-23", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]},   {:date=>"2015-09-24", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]} ]  # convert to_delete array hash { "2015-09-23" => #<set: {"13:30", "12:00"}> } # use set here store unique time values to_delete_as_hash = to_delete.reduce({}) |acc, item|   acc[item[:date]] ||= set.new   acc[item[:date]] += item[:time]   acc end  filtered = data.map |item|   # skip item if there's no need process date   next item unless to_delete_as_hash[item[:date]]   # subtract to_delete values current item[:time] values   item[:time] -= to_delete_as_hash[item[:date]].to_a   item end  p filtered 

output:

[     {:date => "2015-09-21", :time => ["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]},     {:date => "2015-09-22", :time => ["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]},     {:date => "2015-09-23", :time => ["12:30", "13:00", "14:00", "14:30"]},     {:date => "2015-09-24", :time => ["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]} ] 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -