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
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 } } ), a!forEach( items:local!array, expression: reduce(fn!remove,fv!item,{"name","item"}) ) )
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.
I had an idea that question was coming as I saw the duplicate value. LOLThere 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 CommunityBut 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) )
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 ) )
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.
Thanks for the suggestion. Yes. I am using expression rule for this. I am using rule input for passing the parameter as dict/CDT.