Hi,
In Postman flows i need to compare two lists in FQL itself in evaluate block.
Work around is to add multiple for-evaluate-collect blocks for this. But that makes the flow cumbersome when i need to compare different lists in same flow.
Hi,
In Postman flows i need to compare two lists in FQL itself in evaluate block.
Work around is to add multiple for-evaluate-collect blocks for this. But that makes the flow cumbersome when i need to compare different lists in same flow.
Hi @bknathan
What are you trying to compare between the two lists? Just checking to see if they are exactly the same or something more? What do you need the output to be?
Hi @flows-daniel ,
I am trying to compare 2 lists and find their items of all-unique, intersection, only list-1, only in list-2.
For example,
Input to evaluate
List-1 = [a, b, c, d]
List-2 = [c, d, e, f]
Output from evaluate
{
“all-unique” : [a, b, c, d, e, f],
“common”: [c, d],
“only in List-1”: [a, b],
“only in List-2”: [e, f]
}
This will be significantly easier when TypeScript support is released after Post/Con (next week) since we’re really performing 4 different operations here. I will update my answer here after it’s available.
For now you can use this to get the desired result:
{
"all-unique" : $distinct($append(list1, list2)),
"common": $filter($map($distinct($append(list1, list2)), fn ($v) {
$filter($append(list1, list2), fn ($v2) {
$v2 = $v
})[1]
}), fn ($v) {
$v
}),
"only in List-1": $filter($map(list1, fn ($v) {
$v in list2 ? null : $v
}), fn ($v) {
$v
}),
"only in List-2": $filter($map(list2, fn ($v) {
$v in list1 ? null : $v
}), fn ($v) {
$v
})
}
for all the uniques, just combine the lists (using $append) then use $distinct to get the unique ones.
For common ones, we take the unique list and $map through all the elements of the combined list of elements and $filter for only where we found more than one match.
For the only in list 1 and only in list 2, we map over one list to check if the other list contains it, if so we return null, else the element, then filter for all the not nulls.
Working like a charm. Thank you @flows-daniel
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
Updating this with a TypeScript solution as well:
function compareLists(list1: string[], list2: string[]): any {
const allUniqueSet = Array.from(new Set<string>([...list1, ...list2]));
const common: string[] = [];
const onlyInList1: string[] = [];
const onlyInList2: string[] = [];
list1.forEach(item => {
if (list2.includes(item)) {
common.push(item);
} else {
onlyInList1.push(item);
}
});
list2.forEach(item => {
if (!list1.includes(item)) {
onlyInList2.push(item);
}
});
return {
"all-unique": allUniqueSet,
"common": common,
"only in List-1": onlyInList1,
"only in List-2": onlyInList2
};
}
compareLists(list1, list2);
will yield the same result.