Is there a way for me to use a formula to find out if two arrays have a value in common? Basically, I want to show a button if a user has at least one value in their “role_ids” array that matches one or more values in the page’s “roles” array.
In a way, it’s like the opposite of the “distinct” filter. Rather than remove duplicates, I want to either keep them or result a boolean telling me if there are or are not duplicates.
1 Like
I should perhaps also note that the “key” may not be the same, just the values.
You can measure the length(merge(array1,array2)) and compare it against the length(distinct(merge(array1,array2)))
If the lengths match, then there are no matches. If they don’t, then an item was removed with distinct, so there must be a match. (assuming the initial arrays don’t include duplicates. If so, run distinct on them first.)
From your second comment, ““key” may not be the same, just the values,” are you just comparing integers or are these roles inside of an object inside of an array?
Either way, I think you should be able to use some combination of filterByKey or map to condense down the objects into your needed array of just integers, and use them in your comparison formula above.
2 Likes
You can also create a custom formula inside the data panel and then use it where you need. It is just one line of code.
Intersection will return an array with all the elements in common between the two arrays.
hasOneInCommon will return true if at least one element is in both the arrays, false otherwise.
there is a good blog post with code example form common array operations here.
I hope this can be useful.
3 Likes