Skip to main content
lavanyam0003
September 24, 2023
Solved

Combine values of array by same two columns data

  • September 24, 2023
  • 4 replies
  • 0 views

Hi all

I need one suggestion please let me know if anyone have an idea

Question: I had data in array ex:

a!map{

(id:1,idTwo:1,value:text),

(id:1,idTwo:2,value:exam),

(id:1,idTwo:1,value:yes)

},

I need to combine the value when id and idTwo are same

Expected Output:

{

(id:1,idTwo:1,value:textyes),

(id:1,idTwo:2,value:exam)

}

Thanks In Advance

    Best answer by mathieud0001

    Hope this helps:

    a!localVariables(
      local!array: {
        a!map(id: 1, idTwo: 1, value: "text"),
        a!map(id: 1, idTwo: 2, value: "exam"),
        a!map(id: 1, idTwo: 1, value: "yes")
      },
      local!separator: "_",
      local!arrayWithCompositeKey: a!forEach(
        items: local!array,
        expression: a!update(
          fv!item,
          "compositeKey",
          concat(
            fv!item.id,
            local!separator,
            fv!item.idTwo
          )
        )
      ),
      local!uniqueKeys: union(
        local!arrayWithCompositeKey.compositeKey,
        local!arrayWithCompositeKey.compositeKey
      ),
      a!forEach(
        items: local!uniqueKeys,
        expression: a!localVariables(
          local!ids: split(fv!item, local!separator),
          local!values: index(
            local!arrayWithCompositeKey,
            wherecontains(
              fv!item,
              index(local!arrayWithCompositeKey, "compositeKey", {})
            )
          ),
          a!map(
            id: tointeger(local!ids[1]),
            idTwo: tointeger(local!ids[2]),
            value: concat(local!values.value)
          )
        )
      )
    )

    4 replies

    mathieud0001
    September 25, 2023

    Hope this helps:

    a!localVariables(
      local!array: {
        a!map(id: 1, idTwo: 1, value: "text"),
        a!map(id: 1, idTwo: 2, value: "exam"),
        a!map(id: 1, idTwo: 1, value: "yes")
      },
      local!separator: "_",
      local!arrayWithCompositeKey: a!forEach(
        items: local!array,
        expression: a!update(
          fv!item,
          "compositeKey",
          concat(
            fv!item.id,
            local!separator,
            fv!item.idTwo
          )
        )
      ),
      local!uniqueKeys: union(
        local!arrayWithCompositeKey.compositeKey,
        local!arrayWithCompositeKey.compositeKey
      ),
      a!forEach(
        items: local!uniqueKeys,
        expression: a!localVariables(
          local!ids: split(fv!item, local!separator),
          local!values: index(
            local!arrayWithCompositeKey,
            wherecontains(
              fv!item,
              index(local!arrayWithCompositeKey, "compositeKey", {})
            )
          ),
          a!map(
            id: tointeger(local!ids[1]),
            idTwo: tointeger(local!ids[2]),
            value: concat(local!values.value)
          )
        )
      )
    )

    lavanyam0003
    September 25, 2023

    Thanks, It works.

    harshas2775
    September 25, 2023

    If an answer works, it help to mark it as the Answer so as to help other users refer that in case they are stuck with same issue.