Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Filtering empty sections in Slack thread using array::compact alternative

3 replies
Last updated: Jun 11, 2024
"pageSections" : array::compact(pageSections[]->{ defined(postsArray) => {
postsArray[@->slug.current == 'someSlug'][0]->{
_id
},
defined(featuredPost) => {
featuredPost[@->slug.current == ''someSlug'][0]->{
_id
},
},
}
})

output:

pageSections:[…] 2 items
0:{} 0 properties
1:{…} 2 properties
postsArray:{…} 1 property
_createdAt
:2024-06-06T22:43:16ZfeaturedPost:null

Does anyone know why doesn't array::compact work here, it still returns the empty sections?
Jun 10, 2024, 1:03 PM
compact()
only removes nulls, not empty objects
Jun 10, 2024, 6:46 PM
any alternative?
Jun 11, 2024, 4:47 PM
Sure, just filter. Something like:
(pageSections[]->)[postsArray != null || featuredPost != null] {
  ...
}
Jun 11, 2024, 7:14 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?