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
I usually tackle this by breaking it down into pseudocode that describe successive steps of the process, then replacing those with local variables that handle that step (and abstract the functionality behind that variable, which helps in dev and debugging etc).
Here goes my quick "first pass":
Edit: merely for the sake of curiosity, I timed myself at 3 minutes, 4 seconds to translate the above steps into code and whip up this:
a!localVariables( local!initialDict: { {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: rule!RULE_General_distinct(array: local!initialDict.id), /* just does a "union()" on ri!array, on itself, quickly eliminating duplicates */ local!indices: a!forEach( local!uniqueIds, index( wherecontains(fv!item, touniformstring(local!initialDict.id)), 1, {} ) ), index( local!initialDict, local!indices ) )
Thank you Mike