Skip to main content
natarajanm0529
October 28, 2023
Solved

How to keep only unique items based on the specific key. In this case countryId . Data type is List of Dict

  • October 28, 2023
  • 13 replies
  • 0 views

local!test1: {
{
item: "stapler",
name: "Mike",
countryId: 1
},
{
item: "printer",
name: "Larry",
countryId: 2
},
{
item: "laptop",
name: "Mike",
countryId: 1
},
{
item: "glass",
name: "Mike",
countryId: 3
}
},

Best answer by stefanhelzle0001

a!localVariables(
  local!test1: {
    {
      item: "stapler",
      name: "Mike",
      countryId: 1
    },
    {
      item: "printer",
      name: "Larrys",
      countryId: 2
    },
    {
      item: "printer",
      name: "Larry",
      countryId: 2
    },
    {
      item: "laptop",
      name: "Mike",
      countryId: 1
    },
    {
      item: "glass",
      name: "Mike",
      countryId: 3
    }
  },


  a!forEach(
    items: union(local!test1.countryId,local!test1.countryId),
    expression: displayvalue(
      fv!item,
      local!test1.countryId,
      local!test1,
      null
    )
  )
)

13 replies

davidj137213
Brainy
October 28, 2023

Could you give us a little bit more deail about what you need to achive? I,m nor sure If you want to remove the duplicated items, remove the items with a duplicated countryId..... For example, what would you like to get as output in you example?

Anyway, you can get different countries with local!test1.country, and after that, iterate over them (for each), removing the elements no longer needed

natarajanm0529
October 30, 2023

Expected result should unique dict based on specific key. In this case i chose countrId.

local!test1: {
{
item: "stapler",
name: "Mike",
countryId: 1
},
{
item: "printer",
name: "Larry",
countryId: 2
},
{
item: "glass",
name: "Mike",
countryId: 3
}
},

davidj137213
Brainy
October 30, 2023

Which one is the discriminant when you find two items with the same country id (fe Mike)

stefanhelzle0001
Brainy
October 30, 2023

a!localVariables(
  local!test1: {
    {
      item: "stapler",
      name: "Mike",
      countryId: 1
    },
    {
      item: "printer",
      name: "Larrys",
      countryId: 2
    },
    {
      item: "printer",
      name: "Larry",
      countryId: 2
    },
    {
      item: "laptop",
      name: "Mike",
      countryId: 1
    },
    {
      item: "glass",
      name: "Mike",
      countryId: 3
    }
  },


  a!forEach(
    items: union(local!test1.countryId,local!test1.countryId),
    expression: displayvalue(
      fv!item,
      local!test1.countryId,
      local!test1,
      null
    )
  )
)

natarajanm0529
October 30, 2023

Wow! you're amazing. Could you please explain the expression section?

stefanhelzle0001
Brainy
October 30, 2023

From Appian docs, displayvalue: "Tries to match a value in a given array with a value at the same index in a replacement array and returns either the value at the same index or a default value if the value is not found."

https://docs.appian.com/suite/help/23.3/fnc_conversion_displayvalue.html

So it is like: Iterate on unique countries, find the index of the first item in the list of ALL the countries, and return the full record at that same index.

This is a great function once you understand how it works!

November 7, 2023

{
a!localVariables(
local!test1: {
{
item: "stapler",
name: "Mike",
countryId: 1
},
{
item: "printer",
name: "Larry",
countryId: 2
},
{
item: "laptop",
name: "Mike",
countryId: 1
},
{
item: "glass",
name: "Mike",
countryId: 3
}
},
local!unique: union(local!test1.countryId,local!test1.countryId),
local!res: a!forEach(
items: local!unique,
expression: index(local!test1,index(wherecontains(tointeger(fv!item),tointeger(local!test1.countryId)),1,null),null)
),
local!res
)

}