Ruby数组/包含?/。每个
我在尝试确定如何创建所需的阵列时遇到问题 我有一个数组,看起来像这样:Ruby数组/包含?/。每个,ruby,arrays,loops,Ruby,Arrays,Loops,我在尝试确定如何创建所需的阵列时遇到问题 我有一个数组,看起来像这样: [“2013-10-01”、“2013-10-02”、“2013-10-07”] 所以有7天,10-01是星期天,10-07是星期一。我想将每个日期转储到它各自的数组中,但是如果日期不存在(10-03),我想在星期二数组中放置一个空元素 代码Ex。 date_数组=[“2013-10-01”、“2013-10-02”、“2013-10-07”] 星期日数组=[] 星期一数组=[] 星期二数组=[] 星期三数组=[] 星期四数
[“2013-10-01”、“2013-10-02”、“2013-10-07”]
所以有7天,10-01是星期天,10-07是星期一。我想将每个日期转储到它各自的数组中,但是如果日期不存在(10-03),我想在星期二数组中放置一个空元素
代码Ex。
date_数组=[“2013-10-01”、“2013-10-02”、“2013-10-07”]
星期日数组=[]
星期一数组=[]
星期二数组=[]
星期三数组=[]
星期四数组=[]
星期五数组=[]
星期六数组=[]
星期日数组=[]
日期_数组。每个do |日期|
如果日期包括在内?(Date.parse(Time.now.to_s)-39).strftime(“%Y-%m-%d”)
sunday\u array此程序将按您的要求执行。对于每个员工,它创建一个包含false
值的七元素数组,然后将详细信息
数组中每个元素对应的星期几设置为true
。最后,未工作的天数仍然包含false
请注意,Time#wday
为周日返回0
,为周六返回6
,因此员工阵列的每个成员从周日到周六
从输出中可以看出,样本数据中的每个员工都从周一工作到周五,除了Ramille 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
更新
Luigi建议使用groupby
。这个替代版本就是这样写的,我认为它不太可读,但你可能更喜欢它
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
最后一种选择似乎是按照你最近说的做。如果您的需求再次发生变化,请提出一个新问题,全面解释您的需求,并给出一个实际数据的好例子。我花了几分钟寻找一个不存在的bug:Anisa 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"=>[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]}
{"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]}
尝试执行以下操作:
array.group_by{e|Date.parse(e.wday}
为什么要编写如此复杂的代码?:)+1你可以把整个任务都告诉我们,我们会提出更好的解决方案吗?@alex是的。。我同意你的看法。我想你是想用。这肯定更近了,谢谢你的提示。然而,我需要的是一个包含14个值的数组,其中包含每天对应的实际时间(签入、签出、签入、签出等)。我如何提取这些数据?为了澄清我的上述评论,我需要一个类似于您上面的哈希值,其值为签入/签出时间的14个元素数组,与包括真/假的7个元素数组相比。@Luigi:这是新的。我建议您需要一个包含七对时间的数组,每对时间都是该时段的输入和输出时间。你似乎还没有考虑清楚这一点,因为有些员工一天内登录和注销两次<例如,威廉·布朗每天注销两次。您希望如何表示这些数据?@Luigi,您还需要决定是希望报告在之前的七天内运行(在本例中为星期二到星期一),还是如您所说,列始终为星期日到星期六。值得关注的是-对于这些数据的格式,我深表歉意,实际上,在这种情况下,我总是选择晚一点。最终,这些数据将只包括较晚的时间,因此没有人一天签入/签出超过一次。数据将始终在周六晚上运行,从周日到周六,因此列将保持静态。
{"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]}