Save first occurence from an array of elements

Certified Senior Developer

Hello, I need to create rule that takes an array of dictionaries in input and if multiple of these elements have the same "id" value, I want to take only the first one in a separate list:

ri!list:

{

{output: "OK, id: "abcd", description: "hello"},

{output: "OK", id: "abcd", description: "hi"},

{output: "OK", id: "yxz", description: "bye"},

{output: "OK", id: "yxz", description:"goodnight"},

{output: "OK", id: "opt", description: "ok"}

}

the output should be the following:

local!output: 

{
{output: "OK, id: "abcd", description: "hello"}, 
{output: "OK", id: "yxz", description: "bye"},
{output: "OK", id: "opt", description: "ok"}
}


  Discussion posts and replies are publicly visible

Parents
  • You can achieve it using foreach(), index() and wherecontains()

    a!localVariables(
      local!list: {
        {
          output: "OK",
          id: "abcd",
          description: "hello"
        },
        { output: "OK", id: "abcd", description: "hi" },
        { output: "OK", id: "yxz", description: "bye" },
        {
          output: "OK",
          id: "yxz",
          description: "goodnight"
        },
        { output: "OK", id: "opt", description: "ok" }
      },
      local!uniqueIds: union(local!list.id, local!list.id),
      a!forEach(
        items: local!uniqueIds,
        expression: index(
          index(
            local!list,
            wherecontains(fv!item, touniformstring(local!list.id)),
            null
          ),
          1,
          null
        )
      )
    )

Reply
  • You can achieve it using foreach(), index() and wherecontains()

    a!localVariables(
      local!list: {
        {
          output: "OK",
          id: "abcd",
          description: "hello"
        },
        { output: "OK", id: "abcd", description: "hi" },
        { output: "OK", id: "yxz", description: "bye" },
        {
          output: "OK",
          id: "yxz",
          description: "goodnight"
        },
        { output: "OK", id: "opt", description: "ok" }
      },
      local!uniqueIds: union(local!list.id, local!list.id),
      a!forEach(
        items: local!uniqueIds,
        expression: index(
          index(
            local!list,
            wherecontains(fv!item, touniformstring(local!list.id)),
            null
          ),
          1,
          null
        )
      )
    )

Children
No Data