👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Comparing arrays in GROQ query to find intersections.

4 replies
Last updated: Feb 28, 2024
Can someone help me with line 5 - I'm trying to compare two arrays, one is the field allCheckinIds (an array of strings) and the other is a variable $userIds (also an array of strings). I'm trying to see whether there are at least one "intersection" between them.
Feb 27, 2024, 2:04 PM
function checkIntersection(array1, array2) {
    return array1.some(item => array2.includes(item));
}
Not sure if you mean something like this
Feb 27, 2024, 2:06 PM
Correct, but I would like to do it in the GROQ-query itself.
Feb 27, 2024, 2:07 PM
Comparing two arrays will require syntax like seen here , which uses the
count()
function and the parent operator. Presumably, it’ll be something like:

checkinId in $userIds ||
count((allCheckinIds[])[@ in $userIds]) > 0
Feb 27, 2024, 2:47 PM
Great, thank you so much!
Feb 28, 2024, 8:18 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?