Skip to main content
lavanyam0003
Known Participant
Sep 24, 2023 4:01 PM UTC
Solved

Combine values of array by same two columns data

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

    mathieud0001
    Genius
    Sep 25, 2023 12:06 AM UTC

    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
    Genius
    Sep 25, 2023 12:06 AM UTC

    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
    Known Participant
    Sep 25, 2023 5:05 AM UTC

    Thanks, It works.

    harshas2775
    Brainy
    Sep 25, 2023 6:28 AM UTC

    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. 

    Happy to help. Even happier to optimize.