<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Splitting Array based on condition</title><link>https://community.appian.com/discussions/f/rules/20120/splitting-array-based-on-condition</link><description>Hello. I have a list of CDT items where each item has an attribute called: &amp;quot;employeeID&amp;quot;. 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</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Splitting Array based on condition</title><link>https://community.appian.com/thread/78618?ContentTypeID=1</link><pubDate>Mon, 21 Dec 2020 16:40:59 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:129666e8-3a3a-4221-987c-9fe0e02dc90e</guid><dc:creator>Peter Lewis</dc:creator><description>&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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&amp;#39;s an example with some sample data:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  local!data: {
    a!map(
      employeeID: &amp;quot;employee.one&amp;quot;,
      attribute: &amp;quot;abc&amp;quot;
    ),
    a!map(
      employeeID: &amp;quot;employee.one&amp;quot;,
      attribute: &amp;quot;def&amp;quot;
    ),
    a!map(
      employeeID: &amp;quot;employee.two&amp;quot;,
      attribute: &amp;quot;xyz&amp;quot;
    ),
    a!map(
      employeeID: &amp;quot;employee.two&amp;quot;,
      attribute: &amp;quot;123&amp;quot;
    ),
    a!map(
      employeeID: &amp;quot;employee.three&amp;quot;,
      attribute: &amp;quot;abcdef&amp;quot;
    )
  },
  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
        ),
        {}
      )
    )
  )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>