Get union of other fields

Certified Senior Developer

So I have a CDT with a bunch of fields in it. One of the fields is a category, while the other is string. That string field will be identical throughout a particular category, but may or may not be different in different categories. So for example:

local!data: {{category:"round things", string:"bubbles, <otherdata>}, {category:"round things", string:"bubbles, <otherdata>}, {category:"soapy things", string:"bubbles, <otherdata>}, {category:"hard things", string:"rocks, <otherdata>}}

I'd like to get the union of categories for that set, but instead of the output being the categories, it is the strings. 

so something like union(local!data.category, local!data.category) and have the output be: {"bubbles, "bubbles", "rocks"} instead of {"round things", "soapy things", "hard things"}

I hope the idea is clear?

  Discussion posts and replies are publicly visible

  • If the string data is guaranteed to be identical within a category, you could probably make it easy and just return the first item in each category list. Here's an example:

     

    a!localVariables(
      local!data: local!data: {
        {
          category: "round things",
          string: "bubbles, <otherdata>"
        },
        {
          category: "round things",
          string: "bubbles, <otherdata>"
        },
        {
          category: "soapy things",
          string: "bubbles, <otherdata>"
        },
        {
          category: "hard things",
          string: "rocks, <otherdata>"
        }
      },
      local!uniqueCategories: union(touniformstring(local!data.category), touniformstring({})),
      a!forEach(
        items: local!uniqueCategories,
        expression: index(
          local!data.string,
          
          /* Only pulls back the first string from all the categories */
          index(
            
            /* Identifies the rows that match the same category */
            wherecontains(
              tostring(fv!item),
              touniformstring(local!data.category),
            ),
            1,
            {}
          ),
          {}
        )
      )
    )

  • 0
    Certified Senior Developer
    in reply to Peter Lewis

    That sounds like what I need, perfect. Thank you!