My Current Structure:
input:
[
{A:A1, B:B1, C:C1, D:D1, E:E1, F:F1, G:G1, H:H1},
{A:A2, B:B2, C:C2, D:D2, E:E2, F:F2, G:G2, H:H2},
{A:A3, B:B3, C:C3, D:D3, E:E3, F:F3, G:G3, H:H3}
...
]
My Goal:
output
[
{"A":[A1, A2, A3 ...]},
{"B":[B1, B2, B3 ...]},
{"C":[C1, C2, C3 ...]},
{"D":[D1, D2, D3 ...]},
{"E":[E1, E2, E3 ...]},
{"F":[F1, F2, F3 ...]},
{"G":[G1, G2, G3 ...]},
{"H":[H1, H2, H3 ...]}
]
Where "A", "B", "C"... are the literal keys from the original objects, and they are now keys in an object to an array of all values of their type. This is usually simple except for the fact that I need to maintain the keys throughout
This is what I've tried so far but I'm not sure if my loop structure is correct.
def ArrayList invertReponse(ArrayList input){
def output = new ArrayList()
for(i=0;i<input[0].size();i++){
def tempObj = {}
def j=0
input[0].each{ key, value ->
tempObj[[input][0][i]][j] = value
j++
}
output.push(tempObj)
}
return output
}
Map. Is there a particular reason you have to use an list for this?.withDefaulton the starting map.list.inject([:].withDefault{[]}){ m, it -> it.each{ k, v -> m.get(k).add(v)}; m }