ruby根据另一个数组

时间:2015-09-21 11:05:39

标签: ruby

我有两个数据,

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

我尝试将其从

中删除
{: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"]}

我希望得到的输出

{: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"]}

我正在尝试

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

有什么更好的主意吗?

6 个答案:

答案 0 :(得分:1)

如果您可以控制数据的表示方式,那么您可以考虑简化数据:

您要删除的数据

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

要从

中删除的数据
[{:"2015-09-21" => ["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]},
 {:"2015-09-22" => ["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]},
 {:"2015-09-23" => ["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]},
 {:"2015-09-24" => ["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]}]

您想要达到的输出

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

通过这样做你现在可以:

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

input.each do |hash|
  hash.each do |key, value|
    if key == target.keys[0]
      target.values.each do |t_value|
        hash[key].delete(t_value)
      end
    end
  end
end

希望这有帮助。

答案 1 :(得分:0)

首先定义您的测试数据

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:30", "13:00", "14:00", "14:30"]}, {:date=>"2015-09-24", :time=>["12:00", "12:30", "13:00", "13:30", "14:00", "14:30"]}]

to_delete = {:date=>"2015-09-23", :time=>["13:30"]}

主要代码

data.select{|x| x if x[:date] == to_delete[:date]}.each{|x| x.delete(to_delete[:time])}

您还可以在数组中存储to_delete并迭代它。

答案 2 :(得分:0)

试一试:

removeAry.map { |h| h[:date] == "2015-09-23" ? (h[:time].delete("13:30");h) : h }

或:

def removeAry.function(date, time)
  self.map { |h| h[:date] == date ? (h[:time].delete(time);h) : h }
end

removeAry.function("2015-09-23", "13:30")

答案 3 :(得分:0)

我已将代码重写为以下

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"]}]

和操作哈希的函数

def output_hash(data, p_date="2015-09-23", p_time="13:30")
    output = data.collect do |key, value|
      if key[:date] == p_date
         key[:time].delete!(p_time) if key[:time].include?(p_time) 
         time =  key[:time]
         key.delete(:time)
         key.merge!(:time => time)
       else 
         key
       end
    end
    return output
end

将生成以下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:00", "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"]}]

答案 4 :(得分:0)

好的,这就是我要做的事情:

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 to a hash { "2015-09-23" => #<Set: {"13:30", "12:00"}> }
# we use Set here to store only unique time values
to_delete_as_hash = to_delete.reduce({}) do |acc, item|
  acc[item[:date]] ||= Set.new
  acc[item[:date]] += item[:time]
  acc
end

filtered = data.map do |item|
  # skip this item if there's no need to process this date
  next item unless to_delete_as_hash[item[:date]]
  # subtract to_delete values from current item[:time] values
  item[:time] -= to_delete_as_hash[item[:date]].to_a
  item
end

p filtered

输出:

[
    {: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"]}
]

答案 5 :(得分:0)

hash = [
    {: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"]}
]

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

hash.each do |new_hash|
  comparables.each do |comparable|
    if new_hash[:date] == comparable[:date]
      new_hash[:time] = new_hash[:time] - comparable[:time]
    end
  end
end

puts hash

输出

user@ubuntu:~$ ruby test.rb 
{: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"]}