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 } },
Discussion posts and replies are publicly visible
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
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}},
Which one is the discriminant when you find two items with the same country id (fe Mike)
In my real-world application, the specific data of interest to me is solely based on the 'countryId' within the dictionary. The names or other associated information within the dictionaries are irrelevant for my application's purposes.
Try with this code please,
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!res: a!forEach( items: local!test1, expression: { name: index(fv!item, "name"), countryId: index(fv!item, "countryId"), }, ), union(local!res, local!res) )
This solution works if i remove item key. But I don't want to remove items key from dict. Can you give me a solution keeping items also.
Try this
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!res: a!forEach( items: local!test1, expression: { item: index(index(local!test1, index(fv!item, "countryId")), "item"), name: index(fv!item, "name"), countryId: index(fv!item, "countryId"), },
),
union(local!res, local!res)
)
you're trying to replicate the item with the same id and its data. in my application it will change. this solution is not working if add one more entry
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 } }, local!res: a!forEach( items: local!test1, expression: { item: index(index(local!test1, index(fv!item, "countryId")), "item"), name: index(fv!item, "name"), countryId: index(fv!item, "countryId"), }, ), union(local!res,local!res) )