a!localVariables( local!map1: { a!map(Id: "Map 1", key1: "A", key2: "C", key3: "C"), a!map(Id: "Map 2", key1: "C", key2: "A"), a!map(Id: "Map 3", key1: "C", key2: "B", key3: "C") }, local!map2: a!map( Key1: "ky1", Key2: "ky2", Key3: "ky3"
/*This list can be very long e.g. Key4: "ky4", Key5: "ky5" etc.*/
), /*code here*/
/*desired output starts - swap the keys based on mappings in map2 */ /* { a!map(Id: "Map 1" , ky1: "A", ky2: "C" , ky3:"C"), a!map(Id: "Map 2" , ky1: "C", ky2: "A"), a!map(Id: "Map 3" , ky1: "C", ky2: "B" , ky3:"C"), } */ /*desired output ends*/
/*Is it possible ?*/
)
Discussion posts and replies are publicly visible
Using the a!update() function, you can create a new map from scratch. You just need to provide the new keys and the values to copy.
The a!keys() function returns all the key in a map.
a!localVariables( local!map1: { a!map(Id: "Map 1", Key1: "A", Key2: "C", Key3: "C"), a!map(Id: "Map 2", Key1: "C", Key2: "A"), a!map(Id: "Map 3", Key1: "C", Key2: "B", Key3: "C") }, local!map2: a!map(Key1: "ky1", Key2: "ky2", Key3: "ky3"), a!forEach( items: local!map1, expression: a!localVariables( local!map1item: fv!item, a!update( a!map(), a!forEach( items: a!keys(fv!item), expression: if( contains(a!keys(local!map2), fv!item), index(local!map2, fv!item, null()), fv!item ) ), a!forEach( items: a!keys(fv!item), expression: index(local!map1item, fv!item, null()) ) ) ) ) )/* { a!map(Id: "Map 1" , ky1: "A", ky2: "C" , ky3:"C"), a!map(Id: "Map 2" , ky1: "C", ky2: "A"), a!map(Id: "Map 3" , ky1: "C", ky2: "B" , ky3:"C"), } */
A slightly different approach using the displayvalue function.
a!localVariables( local!map1: { a!map(Id: "Map 1", Key1: "A", Key2: "C", Key3: "C"), a!map(Id: "Map 2", Key1: "C", Key2: "A"), a!map(Id: "Map 3", Key1: "C", Key2: "B", Key3: "C") }, local!map2: a!map(Key1: "ky1", Key2: "ky2", Key3: "ky3"), a!forEach( items: local!map1, expression: a!update( /* Populate an empty map */ a!map(), /* Define the keys, trying to replace key names */ a!forEach( items: a!keys(fv!item), expression: displayvalue( fv!item, a!keys(local!map2), index(local!map2, a!keys(local!map2)), fv!item ) ), /* Picking all values from the list item */ index(fv!item, a!keys(fv!item), null()) ) ) )
Thanks Stefan. I don't need to iterate over values part. my sleepy head did not catch that.