Remove duplicate from list of objects with FQL?

Hi, I am having problems trying to figure out how to remove duplicates from a list of objects. I am using the $distinct but only return a list of values. if there another function that I could use?

I have been reading the docs but I don’t see any useful function for this case. I guess my question how can I decided which property to use in order to remove the duplicates

Hi @supply-candidate-367

In your dataset returned, there are a list of objects. Here, you are only looking for a unique costCode. Is that the only unique feature or will the other fields match as well?

If all the fields match, you can use $distinct(costcodeList) and it should return a list of the unique objects.

If only the costCode matches, which matching item would you like from the list (i.e. the costCode is the same but the title might be different, which title would you want)

Sorry, I guess I did the misleading question. My problem is this: I have a list of objects with the following structure:

id, costcodeId, title, costcodeGroupId.

I know that in this list there are duplicate items with the same “costcodeId”, I want to remove those and only create a list with unique items with costcodeId. But when I used “$distinc” I got the same QTY.

image

The function $distinct will not work in this case because you want to check for uniqueness on a particular key as opposed to the entire object. The current implementation of distinct checks for object equality. So in the first screenshot below, one of the records is eliminated but if the records are not identical, you will get both records as in the second screenshot.


As @flows-daniel asked, if you have two objects that share your primary key costcodeId but are not identical records, which one do you want to filter out?

ooooooooooooh, now I understand how $distinct works. Thanks for the explanation. you are right I can not use it

So… How can I remove duplicates from a list based on 1 property value?

@supply-candidate-367

If only one property value is unique, how should it be determined which object to keep and which to remove as a duplicate? If there are two objects that have the same costcodeId but a different title, which title should be kept?

if there are 5 objects with the same “costcodeId” you should remove 4 and only leave 1, independently of the value of the other’s properties. That is my business use case

Try this:

$map($distinct(list.costCode[]), fn ($v) {
    $filter(list, fn ($v2) {
        $v2.costCode = $v
    })[0]
})

First, we use $distinct on the costCodes and return it as a list using list.costCode[], where list is just the variable name for the input to the evaluate block. Then $map over those entries and for each distinct value and then $filter the original list for a record that has a matching codeCode and return the first match in the list (since you stated which object is returned doesn’t matter).

oooh looks great thanks.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.