how to remove multiple key from this list of dictionary. I did it like this i want some better solution using remove

a!localVariables(
  local!array: cast(
    97,
    {
      {
        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!updatedArray: a!forEach(
    items: local!array,
    expression: remove(fv!item,"item")
  ),
  local!finalArray: a!forEach(
    items: local!updatedArray,
    expression: remove(fv!item,"name")
  ),
  local!finalArray
)

  Discussion posts and replies are publicly visible

Parents Reply Children
  • Thanks Mate !. Works like a charm. One more QQ how remove duplicate dictionary based on key value pair . In this data i want to remove countryId: 1 . it occurs twice.

  • 0
    Certified Senior Developer
    in reply to natarajanmuthu

    I had an idea that question was coming as I saw the duplicate value. LOL

    There is a plugin to do this on the App Market which I haven't personally tried, and it works on CDTs and it may work on data dictionaries:

    (+) Appian Community

    B
    ut you can also try:

    a!localVariables(
      local!array: cast(
        97,
        {
          {
            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!filteredArray:a!forEach(
        items:local!array,
        expression: reduce(fn!remove,fv!item,{"name","item"})
      ),
      union(local!filteredArray,local!filteredArray)
    
    )

  • it works only if i remove every key name {"name","item"} from the dict. So it only checks the countryId. But in my real application .. this is not the scenario. Let me give the updated code. i only removed one key. Now i want to remove duplicate entry. this code doesnt work. 

    a!localVariables(
      local!array: cast(
        97,
        {
          {
            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!filteredArray:a!forEach(
        items:local!array,
        expression: reduce(fn!remove,fv!item,{"name"})
      ),
      union(local!filteredArray,local!filteredArray)
    
    
    )

  • +1
    Certified Senior Developer
    in reply to natarajanmuthu

    Ok, your dictionary is actually not quite duplicate since country ids are the same but items are not the same, but if you really want to remove one of the duplicate countryIds, I don't know which one, try this code and see my comments:

    a!localVariables(
      local!array: cast(
        97,
        {
          {
            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!filteredArray:a!forEach(
        items:local!array,
        expression: reduce(fn!remove,fv!item,{"name"})
      ),
      /*Find the duplicate items indexes*/
      local!duplicateCountryIdsIndexes: where(
        a!forEach(
          items: local!filteredArray,
          expression: contains(
            remove(local!filteredArray.countryId, fv!index),
            fv!item.countryId,
    
          )
        )
      ),
      
      /*To return the duplicae items*/
      local!duplicateItems: local!filteredArray[local!duplicateCountryIdsIndexes],
    
      /* Remove one of the duplicates, let's say the last item as I don't know which one you need
         to keep. length(local!duplicateCountryIdsIndexes) will give you the last item, if you want to keep
         the first item simply use 1 instead of length(local!duplicateCountryIdsIndexes)
      */
      local!duplicateItemToRemove:if(
        a!isNotNullOrEmpty(local!duplicateItems),
        remove(local!duplicateItems,length(local!duplicateCountryIdsIndexes)),
        null
      ),
    
      difference(
        local!filteredArray,
        local!duplicateItemToRemove
      )
    
    )

  • 0
    Certified Senior Developer
    in reply to amir.karami

    I also suggest creating an expression rule to do this and call the expression rule by passing in your dictionary/CDT. That will make is reusable.

  • This is what i was expecting. Thank you so much. I like this approach. I have other idea. Since I am new to this platform i don't how to leverage appian function efficiently . 

    My idea:- Iterate over dictionary extract all the Country Id in the set {1,2,3} it will remove the duplicates. then check if the set value present in countryId. If not present remove that whole dictionary. I am not sure how to implement this. If its in python . two line of code.

  • Thanks for the suggestion. Yes. I am using expression rule for this. I am using rule input for passing the parameter as dict/CDT.