Splitting Array based on condition

Hello. I have a list of CDT items where each item has an attribute called: "employeeID". I want to split the original array into sub-arrays based on distinct values of employeeID; where each sub-array contains the items corresponding to the employeeID. Is this possible using Appian?

  Discussion posts and replies are publicly visible

  • For something like this, I usually like to separate what you need into distinct steps. The main things that you want to do here are to (1) create a list of distinct values for the employeeID and (2) construct a new list based on these values that matches the original data.

    The first step usually involves the union() function. This function allows you to get a unique list of values by defining a union against an empty array.

    Then, the second step requires you to loop over the list of values and match the other attributes with the corresponding employeeID. I usually use a combination of wherecontains() and index() to return the corresponding items here. Here's an example with some sample data:

    a!localVariables(
      local!data: {
        a!map(
          employeeID: "employee.one",
          attribute: "abc"
        ),
        a!map(
          employeeID: "employee.one",
          attribute: "def"
        ),
        a!map(
          employeeID: "employee.two",
          attribute: "xyz"
        ),
        a!map(
          employeeID: "employee.two",
          attribute: "123"
        ),
        a!map(
          employeeID: "employee.three",
          attribute: "abcdef"
        )
      },
      local!uniqueEmployees: union(
        local!data.employeeID,
        touniformstring({})
      ),
      a!forEach(
        items: local!uniqueEmployees,
        expression: a!map(
          employeeID: fv!item,
          attributes: index(
            local!data.attribute,
            wherecontains(
              fv!item,
              local!data.employeeID
            ),
            {}
          )
        )
      )
    )