🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Filtering an array of strings in a Sanity.io project

23 replies
Last updated: May 30, 2023
Anyone able to help me here? I'm trying to filter an array (
platform
) of strings. Tried several ways but they keep returning null
example of what platform would look like ['ios', 'android']


*[_type == 'bannerQueue'] {
  banners[]->{
    _id,
    bannerType,
    "description": description[$lang],
    "imageUrl": image.asset->url,
    platform,
  }
}[0]
May 30, 2023, 11:23 PM
Are you getting
null
or an array of `null`s?
May 30, 2023, 11:24 PM
just null
May 30, 2023, 11:25 PM
*[_type == 'bannerQueue' && "android" in banners[]->platform] {
  banners[]->{
    _id,
    bannerType,
    "description": description[$lang],
    "imageUrl": image.asset->url,
    platform,
  }
}[0]
One of the things I've tried
May 30, 2023, 11:25 PM
Is
bannerQueue
(case-sensitive) a valid document type?
May 30, 2023, 11:25 PM
yes it is
May 30, 2023, 11:25 PM
Oh, is that second one your actual query?
May 30, 2023, 11:25 PM
The first sample code I provided return correct results unfiltered
May 30, 2023, 11:25 PM
If
platform
is an array, can you please try:

*[_type == 'bannerQueue' && "android" in banners[]->platform[]]
May 30, 2023, 11:26 PM
That just returns all the results again unfiltered
May 30, 2023, 11:26 PM
It’s not limiting the documents to just those with “android” in the
platform
array?
May 30, 2023, 11:27 PM
nope!
May 30, 2023, 11:27 PM
Index 1 doesn't contain android
May 30, 2023, 11:28 PM
this kind of nesting always confuses me. I do something similar but don’t quite remember if this is the solution…

*[_type == 'bannerQueue' && count((banners[]->platform)["android" in @]) > 0]
May 30, 2023, 11:31 PM
It looks like your
bannerQueue
document contains an array with two banners. Your current filter is being met, since at least one of those banners contains ‘android’. You need to move the filter down to the banners array itself.
May 30, 2023, 11:36 PM
I've tried a few ways in the banner array itself as well. Is there an issue with my syntax?

*[_type == 'bannerQueue'] {
  banners["android" in platform[]]->{
    _id,
    bannerType,
    "description": description[$lang],
    "imageUrl": image.asset->url,
    platform,
  }
}[0]
May 30, 2023, 11:40 PM
Ah. I see. The filter we’ve been adjusting is for the document(s) returned, which in your case, it’s doing (and just the one document). You’ll want to filter the items returned by the
banners
array. I think you’ll need something like this to limit your
banners
array:

*[_type == 'bannerQueue' && "android" in banners[]->platform[]] {
  banners["android" in @->banners[].platform[]]->{
    _id,
    bannerType,
    "description": description[$lang],
    "imageUrl": image.asset->url,
    platform,
  }
}[0]
This feels like it won’t be very performant, and it’s not tested, but I’ll try to loop back to it later.
May 30, 2023, 11:40 PM
user M
Yep that makes sense!
May 30, 2023, 11:40 PM
Ideally the
"android" in…
stuff would come after the dereference and allow that instance of the dereference to be removed, but not sure if that’s possible.
May 30, 2023, 11:41 PM
Unfortunately still getting no results 😢
May 30, 2023, 11:42 PM
Sorry. This:

banners["android" in @->banners[].platform[]]->{
Should be this:


banners["android" in @->platform[]]->{
May 30, 2023, 11:42 PM
That works!
May 30, 2023, 11:43 PM
Great! Thanks for confirming. I’ll still see if there might be a more performant approach, but I’m glad to hear you’re able to filter the data correctly. 🙌
May 30, 2023, 11:44 PM
Appreciate the help!! 🙌
May 30, 2023, 11:45 PM

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?