该程序将按照您的要求进行。它为每个员工创建一个包含 7 个元素的false
值数组,然后将数组中每个元素对应的星期几details
设置为true
。最后,未工作的日子仍然包含false
.
请注意,周日和周六Time#wday
返回,因此每个员工数组的成员从周日到周六。0
6
从输出中可以看出,样本数据中的每个员工从周一到周五都在工作,除了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]}