0

我在尝试确定如何创建我需要的数组时遇到问题。

我有一个看起来像这样的数组:

["2013-10-01","2013-10-02","2013-10-07"]

所以有 7 天,10-01 是星期日,10-07 是星期一。我想将每个日期转储到其各自的数组中,但如果日期不存在 (10-03),我想在 tuesday 数组中放置一个空元素。

代码前。

date_array = ["2013-10-01","2013-10-02","2013-10-07"]
sunday_array = []
monday_array = []
tuesday_array = []
wednesday_array = []
thursday_array = []
friday_array = []
saturday_array = []
sunday_array = []

date_array.each do |date|
  if date.include? (Date.parse(Time.now.to_s) - 39).strftime("%Y-%m-%d")
    sunday_array << date
  elsif date.include? (Date.parse(Time.now.to_s) - 38).strftime("%Y-%m-%d")
    monday_array << date
  elsif date.include? (Date.parse(Time.now.to_s) - 37).strftime("%Y-%m-%d")
    tuesday_array << date
  #etc, etc.
  end
end

这会起作用,但它不会创建我需要的空值。最后,每个数组应该有完全相同数量的元素,即使thursday_array有 100% 的空值。

4

2 回答 2

1

该程序将按照您的要求进行。它为每个员工创建一个包含 7 个元素的false值数组,然后将数组中每个元素对应的星期几details设置为true。最后,未工作的日子仍然包含false.

请注意,周日和周六Time#wday返回,因此每个员工数组的成员从周日到周六。06

从输出中可以看出,样本数据中的每个员工从周一到周五都在工作,除了Ramielle Ford周五也请假的员工。

包含该pp模块只是为了证明pp可以很好地布置数据的命令。你将不需要它。

require 'date'
require 'pp'

table = eval(<<__END__JSON__)
{"success"=>true, "data"=>[
    {"id"=>133572, "memberId"=>"103001862-07", "firstName"=>"Anissa", "lastName"=>"Martin", "dateOfBirth"=>"2002-11-09", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"08:19:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"08:15:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:40:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"08:26:00", "checkOutTime"=>"08:00:00"}]},
    {"id"=>133573, "memberId"=>"103001862-08", "firstName"=>"Allyson", "lastName"=>"Martin", "dateOfBirth"=>"2005-02-16", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"08:19:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"08:15:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:40:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"08:26:00", "checkOutTime"=>"08:00:00"}]},
    {"id"=>135692, "memberId"=>"103008263-05", "firstName"=>"Emmya", "lastName"=>"Burrell", "dateOfBirth"=>"2003-01-17", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"16:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"16:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"16:40:00"}]},
    {"id"=>138343, "memberId"=>"103015748-03", "firstName"=>"Abigail", "lastName"=>"Young", "dateOfBirth"=>"2003-08-09", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"07:40:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"07:35:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:36:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>1, "checkInTime"=>"07:40:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:30:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>1, "checkInTime"=>"07:30:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"07:56:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:26:00"}]},
    {"id"=>139451, "memberId"=>"103016684-03", "firstName"=>"William", "lastName"=>"Brown", "dateOfBirth"=>"2004-02-06", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}]},
    {"id"=>8748758, "memberId"=>"2162539-05", "firstName"=>"Ramielle", "lastName"=>"Ford", "dateOfBirth"=>"2009-08-06", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}]},
    {"id"=>8795043, "memberId"=>"2163877-04", "firstName"=>"Christopher", "lastName"=>"Lampkins", "dateOfBirth"=>"2006-11-26", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:23:00"}]}]}
__END__JSON__

cards = {}
table['data'].each do |staff|
  name = staff.values_at(*%w/ firstName lastName /).join(' ')
  cards[name] = Array.new(7, false)
  staff['details'].each do |clock|
    wday = Date.parse(clock['date']).wday
    cards[name][wday] = true
  end
end

pp cards

输出

{"Anissa Martin"=>[false, true, true, true, true, true, false],
 "Allyson Martin"=>[false, true, true, true, true, true, false],
 "Emmya Burrell"=>[false, true, true, true, true, true, false],
 "Abigail Young"=>[false, true, true, true, true, true, false],
 "William Brown"=>[false, true, true, true, true, true, false],
 "Ramielle Ford"=>[false, false, true, true, true, false, false],
 "Christopher Lampkins"=>[false, true, true, true, true, true, false]}

更新

Luigi 建议使用group_by. 这个替代版本是这样写的,我认为它的可读性较差,但您可能更喜欢它。

cards = {}
table['data'].each do |staff|
  name = staff.values_at(*%w/ firstName lastName /).join(' ')
  cards[name] = Array.new(7, false)
  staff['details'].group_by { |clock| Date.parse(clock['date']).wday }.each_key do |wday|
    cards[name][wday] = true;
  end
end

更新 2

这个最后的选择似乎做了你最近说的你想要的。如果您的需求再次发生变化,请打开一个新问题,全面解释您的需求,并提供一个很好的实际数据示例。我花了几分钟寻找一个不存在的错误:Anissa Martin 确实8:26 打卡,8:00 打卡。

这个迭代与我的原作相差不远,如果你有足够的知识有机会为自己写这篇文章,那么很难相信你不可能想出同样的东西。

cards = {}
table['data'].each do |staff|
  name = staff.values_at(*%w/ firstName lastName /).join(' ')
  cards[name] = Array.new(14, nil)
  staff['details'].each do |clock|
    wday = DateTime.parse(clock['date']).wday
    cards[name][wday + wday, 2] = clock.values_at(*%w/ checkInTime checkOutTime /)
  end
end

输出

{"Anissa Martin"=>
  [nil,
   nil,
   "08:26:00",
   "08:00:00",
   "08:19:00",
   "08:00:00",
   "08:15:00",
   "08:00:00",
   "15:30:00",
   "17:40:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil],
 "Allyson Martin"=>
  [nil,
   nil,
   "08:26:00",
   "08:00:00",
   "08:19:00",
   "08:00:00",
   "08:15:00",
   "08:00:00",
   "15:30:00",
   "17:40:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil],
 "Emmya Burrell"=>
  [nil,
   nil,
   "15:30:00",
   "16:40:00",
   "15:30:00",
   "16:00:00",
   "15:30:00",
   "16:00:00",
   "07:00:00",
   "08:00:00",
   "07:00:00",
   "08:00:00",
   nil,
   nil],
 "Abigail Young"=>
  [nil,
   nil,
   "15:30:00",
   "17:26:00",
   "15:30:00",
   "17:00:00",
   "15:30:00",
   "17:36:00",
   "15:30:00",
   "17:30:00",
   "07:30:00",
   "08:00:00",
   nil,
   nil],
 "William Brown"=>
  [nil,
   nil,
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil],
 "Ramielle Ford"=>
  [nil,
   nil,
   nil,
   nil,
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil,
   nil,
   nil],
 "Christopher Lampkins"=>
  [nil,
   nil,
   "15:30:00",
   "17:23:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil]}
于 2013-11-09T16:57:33.270 回答
0

尝试执行以下操作:array.group_by{|e| Date.parse(e).wday }

http://www.ruby-doc.org/core-2.0.0/Time.html#method-i-wday

于 2013-11-09T17:09:03.003 回答