0
我有一个数组,这是我从网络服务中获得所以它总是不同的,但是看起来像这样动态数组:的Groovy/Grails的 - 修改基于内容
def originalArray = [
[startDate:7 October 2017, endDate:14 October 2017, type:A],
[startDate:7 October 2017, endDate:9 October 2017, type:A],
[startDate:9 October 2017, endDate:14 October 2017, type:A],
[startDate:7 October 2017, endDate:14 October 2017, type:B],
[startDate:7 October 2017, endDate:9 October 2017, type:C],
[startDate:9 October 2017, endDate:14 October 2017, type:C]]
不介意约会“格式”使用,我写这样就简化了这个问题。我想要做的是过滤类型类型,但也有发生的周全周,周中和周末都发生在同一周,并具有相同的类型它应该删除周中和周末和给整周一个名为的可拆分项设置为true。所以,上面的例子最终会看起来像这样:
def moddedArray = [
[startDate:7 October 2017, endDate:14 October 2017, type:A, splittable:true],
[startDate:7 October 2017, endDate:14 October 2017, type:B, splittable:false],
[startDate:7 October 2017, endDate:9 October 2017, type:C, splittable:false],
[startDate:9 October 2017, endDate:14 October 2017, type:C, splittable:false]]
这是我已经试过:
function startingFunction() {
def weeks = getWeeks() // gets the array
def fullWeeks = []
weeks.each {
def days = it.endDate - it.startDate
if(days == 7) {
fullWeeks.add(it)
}
}
if(fullWeeks) {
return applySplittable(fullWeeks,weeks)
} else {
return weeks
}
}
function applySplittable(fullweeks,weeks) {
def prelimWeeks = []
def weeksSinglesRemoved = []
fullweeks.eachWithIndex { f, i ->
weeks.eachWithIndex { w, j ->
if(!(f.type == w.type && (f.startDate == w.startDate || f.endDate == w.endDate) && (!(f.startDate == w.startDate && f.endDate == w.endDate)))) {
prelimWeeks.add(w)
}
}
}
prelimWeeks.each { // when it gets here all the weeks will have been added as many times as there fullweeks, unless it's a midweek or weekend that have a fullweek
if(Collections.frequency(prelimWeeks,it) >= fullweeks.size()) {
weeksSinglesRemoved.add(it)
}
}
return weeksSinglesRemoved.unique()
}
因此,大家可以看到,我已经试过是一个烂摊子,可能是不即使这样很有效,但如果有一整周的时间,它可以消除中周和周末,但它不适用可拆分属性。如果任何人都可以为我提供一种尽可能简单地完成这项工作的方法。让我知道是否需要提供更多信息。