a!map complex merging

Input map

local!inputMap:{
a!map(pagenum: 1,fieldname: "Name", fieldValue:"Appian",docid:1),
a!map(pagenum: 1,fieldname: "howold", fieldValue:15, docid:1),
a!map(pagenum: 1,fieldname: "Name", fieldValue:"VB",docid:1),
a!map(pagenum: 1,fieldname: "howold", fieldValue:12, docid:1),
a!map(pagenum: 2,fieldname: "Name", fieldValue:"Java",docid:1),
a!map(pagenum: 2,fieldname: "howold", fieldValue:25, docid:1),
a!map(pagenum: 1,fieldname: "Name", fieldValue:"COBOL",docid:2),
a!map(pagenum: 1,fieldname: "howold", fieldValue:45, docid:2),
a!map(pagenum: 2,fieldname: "Name", fieldValue:"PL1",docid:2),
a!map(pagenum: 2,fieldname: "howold", fieldValue:55, docid:2)
},

Desired output map


local!outputMap :{
a!map(pagenum: 1 , Name: "Appian", howold: 15, docid:1),

a!map(pagenum: 1 , Name: "VB", howold: 12, docid:1),
a!map(pagenum: 2 , Name: "Java", howold: 25, docid:1),
a!map(pagenum: 1 , Name: "COBOL", howold: 45, docid:2),
a!map(pagenum: 2 , Name: "PL1", howold: 55, docid:2)
},

Is this possible ?

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Sure. The trick here is to create a new map and add the fields iteratively. The reduce function allows to apply the same function and modify the data on the go. The "_" is a placeholder at design time and replaced by the actual data at runtime.

    docs.appian.com/.../expression-advanced-evaluation.html

    a!localVariables(
      local!input: {
        a!map(pagenum: 1,fieldname: "Name", fieldValue:"Appian",docid:1),
        a!map(pagenum: 1,fieldname: "howold", fieldValue:15, docid:1),
        a!map(pagenum: 2,fieldname: "Name", fieldValue:"Java",docid:1),
        a!map(pagenum: 2,fieldname: "howold", fieldValue:25, docid:1)
      },
      a!forEach(
        items: union(local!input.pagenum, local!input.pagenum),
        expression: a!localVariables(
          local!rows: index(local!input, where(local!input.pagenum = fv!item), {}),
          reduce(
            a!update(_, _, _),
            a!map(pagenum: fv!item),
            merge(local!rows.fieldname, local!rows.fieldValue)
          )
        )
      )
    )

Reply
  • 0
    Certified Lead Developer

    Sure. The trick here is to create a new map and add the fields iteratively. The reduce function allows to apply the same function and modify the data on the go. The "_" is a placeholder at design time and replaced by the actual data at runtime.

    docs.appian.com/.../expression-advanced-evaluation.html

    a!localVariables(
      local!input: {
        a!map(pagenum: 1,fieldname: "Name", fieldValue:"Appian",docid:1),
        a!map(pagenum: 1,fieldname: "howold", fieldValue:15, docid:1),
        a!map(pagenum: 2,fieldname: "Name", fieldValue:"Java",docid:1),
        a!map(pagenum: 2,fieldname: "howold", fieldValue:25, docid:1)
      },
      a!forEach(
        items: union(local!input.pagenum, local!input.pagenum),
        expression: a!localVariables(
          local!rows: index(local!input, where(local!input.pagenum = fv!item), {}),
          reduce(
            a!update(_, _, _),
            a!map(pagenum: fv!item),
            merge(local!rows.fieldname, local!rows.fieldValue)
          )
        )
      )
    )

Children
No Data