如何从API分组日期和时间数据

时间:2017-04-10 19:15:25

标签: ruby date parsing ruby-hash

我正在尝试将从API获取的数据分组到我们的前端应用程序。我的意思是小组"时间" by" date"。 日期:{date1: [time1, time2, timeN], date2: [time1...]}

我的输入是这样的:

{"date"=>"2017-04-04T00:00:00", "time"=>"1754-01-01T13:00:00"}
{"date"=>"2017-04-04T00:00:00", "time"=>"1754-01-01T14:00:00"}
{"date"=>"2017-04-05T00:00:00", "time"=>"1754-01-01T12:00:00"}
{"date"=>"2017-04-05T00:00:00", "time"=>"1754-01-01T13:00:00"}

我的输出应该是这样的:

dates: [{date: "2017-04-04T00:00:00", availableTimes: ["1754-01-01T13:00:00", "1754-01-01T14:00:00"]}, {date: "2017-04-05T00:00:00", availableTimes: ["1754-01-01T12:00:00", "1754-01-01T13:00:00"]}]

我试图这样做,但没有陷入循环疯狂。我有以下内容:

dates  = Hash[input_data.map{|sd| [sd.date, [""]]}]

这给了我这样的数据输出:

{"2017-04-04T00:00:00"=>[""],
 "2017-04-05T00:00:00"=>[""],
 "2017-04-11T00:00:00"=>[""],
 "2017-04-12T00:00:00"=>[""],
 "2017-04-18T00:00:00"=>[""],
 "2017-04-19T00:00:00"=>[""],
 "2017-04-25T00:00:00"=>[""],
 "2017-04-26T00:00:00"=>[""]}

5 个答案:

答案 0 :(得分:3)

只有一种可能的方式:

input.each_with_object(Hash.new { |h, k| h[k] = [] }) do |h, m|
  m[h['date']] << h['time']
end.map { |k, v| { date: k, avaliable_times: v } }
#=> [{:date=>"2017-04-04T00:00:00", :avaliable_times=>["1754-01-01T13:00:00", "1754-01-01T14:00:00"]},
#    {:date=>"2017-04-05T00:00:00", :avaliable_times=>["1754-01-01T12:00:00", "1754-01-01T13:00:00"]}]

实际上,似乎您的数据结构在没有最后map的情况下会更简洁,我的意思是:

#=> {"2017-04-04T00:00:00"=>["1754-01-01T13:00:00", "1754-01-01T14:00:00"],
#    "2017-04-05T00:00:00"=>["1754-01-01T12:00:00", "1754-01-01T13:00:00"]}

答案 1 :(得分:1)

有很多方法可以做到这一点,一种方法是创建一个新的哈希,并将默认值设置为一个数组,然后遍历结果并插入日期:

dates = Hash.new { |hash, key| hash[key] = [] }
input_data.each{ |sd| dates[sd["date"]] << sd["time"] }

答案 2 :(得分:1)

您正在获取该输出,因为您的map函数实际上并未修改任何类型的数据结构。它只是返回一个新的数组,其中包含包含日期的数组和一个包含空字符串的数组。基本上,这不会仅通过一次地图调用来完成。

因此,基本算法将是:

  1. 查找所有唯一日期的数组
  2. 循环显示唯一日期并使用select仅在循环迭代中获取当前日期的日期/时间对
  3. 以您喜欢的格式设置数据
  4. 此代码的filteredDates格式为您需要的数据

    filteredDates = { dates: [] }
    uniqueDates = input_data.map { |d| d["date"] }.uniq # This is an array of only unique dates
    uniqueDates.each do |date|
        dateTimes = input_data.select { |d| d["date"] == date }
        newObj = { date: date }
        newObj[:availableTimes] = dateTimes.map { |d| d["time"] }
        filteredDates[:dates].push(newObj)
    end
    

    以下是filteredDates的样子:

    {:dates=>[{:date=>"2017-04-04T00:00:00", :availableTimes=>["1754-01-01T13:00:00", "1754-01-01T14:00:00"]}, {:date=>"2017-04-05T00:00:00", :availableTimes=>["1754-01-01T12:00:00", "1754-01-01T13:00:00"]}]}
    

答案 3 :(得分:1)

我会使用Enumerable#group_by

dates = [{"date"=>"2017-04-04T00:00:00", "time"=>"1754-01-01T13:00:00"},
         {"date"=>"2017-04-04T00:00:00", "time"=>"1754-01-01T14:00:00"},
         {"date"=>"2017-04-05T00:00:00", "time"=>"1754-01-01T12:00:00"},
         {"date"=>"2017-04-05T00:00:00", "time"=>"1754-01-01T13:00:00"}]

dates.group_by { |g| g["date"] }.
      map { |k,v| { date: k, available_times: v.map { |h| h["time"] } } }
  #=> [{:date=>"2017-04-04T00:00:00",
  #     :available_times=>["1754-01-01T13:00:00", "1754-01-01T14:00:00"]},
  #    {:date=>"2017-04-05T00:00:00",
  #     :available_times=>["1754-01-01T12:00:00", "1754-01-01T13:00:00"]}] 

第一步产生以下中间值:

dates.group_by { |g| g["date"] }      
  #=> {"2017-04-04T00:00:00"=>
  #     [{"date"=>"2017-04-04T00:00:00", "time"=>"1754-01-01T13:00:00"},
  #      {"date"=>"2017-04-04T00:00:00", "time"=>"1754-01-01T14:00:00"}],
  #    "2017-04-05T00:00:00"=>
  #     [{"date"=>"2017-04-05T00:00:00", "time"=>"1754-01-01T12:00:00"},
  #      {"date"=>"2017-04-05T00:00:00", "time"=>"1754-01-01T13:00:00"}]}

答案 4 :(得分:0)

可能有更优雅的方式,但

results = Hash.new
dates.each do |date|
  d, t = date['date'].split('T') # (clean up/split date and time formatting)
  results.key?(d) ? nil : results[d] = Array.new
  results[d] << t
end
puts results
# => {"2017-04-04"=>["13:00:00", "14:00:00"], "2017-04-05"=>["12:00:00", "13:00:00"]}