Groovy/Grails - 根据内容修改动态数组

Groovy/Grails - Modify dynamic array based on content

我有一个数组,它是从网络服务中获取的,所以它总是不同但看起来像这样:

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]]

不要介意使用的日期 "formats",我这样写是为了简化这个问题。我想做的是通过 type 过滤这个数组,但当出现整周、周中和周末都发生在同一周并且具有相同的 type 它应该删除周中和周末并给全周一个名为 splittable 的 属性 设置为 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()
}

正如你所看到的,我尝试的是一团糟,甚至可能没有那么有效,但如果有一个完整的星期,它可以删除周中和周末,但它不应用可拆分 属性.如果有人能给我提供一种方法来尽可能简单地完成这项工作。如果我需要提供更多信息,请告诉我。

我更新了 applySplittable 函数,现在看起来像这样:

def applySplittable(fullweeks, weeks) {
    def weeksSplit = []
    weeks.eachWithIndex { w, i ->
        def days = w.endDate - w.startDate
        if(days != 7) {
            def hasFull = fullweeks.findAll { w.type == it.type && (w.startDate == it.startDate || w.endDate == it.endDate) }
            if(!hasFull) {
                w.splittable = false
                weeksSplit.add(w)
            }
        } else {
            def hasSplit = weeks.findAll { w.type == it.type && (w.startDate == it.startDate || w.endDate == it.endDate) && !(w.startDate == it.startDate && w.endDate == it.endDate) }
            if(hasSplit) {
                w.splittable = true
            } else {
                w.splittable = false
            }
            weeksSplit.add(w)
        }
    }
    return weeksSplit
}

它仍然是一团糟,它可能仍然很低效,但它似乎在工作。