Groovy web console

subscribe to the feed Subscribe
to this
site
merge lists (via #groovywebconsole)
tweet this snippet Tweet
this
script

merge lists

Published 2 months ago by nmrao with tags merge
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def list1 = [[project_no:'0-10001',case_no:'00492268'],[project_no:'0-10160',case_no:'01957580'],[project_no:'1-10014',case_no:'02022686']]
def list2 = [[project_no:'0-10160',case_no:'01957590'],[project_no:'1-10014',case_no:'019126'],[project_no:'1-2896337',case_no:'02039596']]

def result = list1+list2
def projNos = result.inject([]) { keys, item ->  keys << item.project_no } as Set


def finalResult = []
projNos.each { proj ->
    def cases = []
    result.each { item ->
        if (item.project_no == proj) {
            cases << item.case_no
        }
    }
    finalResult << [ 'project_no': proj, 'case_no' : cases ]
}

println finalResult