Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Using GROQ to achieve shorthand string compare within array::join function

1 replies
Last updated: Jun 20, 2023
Hey folks!
I’m wondering if it is possible to somehow achieve the same result as a short-hand string compare using GROQ within an
array::join
function like this:

*[_id in [$published, $draft]] {
  "slug": "/" + array::join(
    array::compact([
      language=="en" ? null : language,
      parent->parent->slug.current,
      parent->slug.current,
      slug.current
    ]), "/"
  )
}
Jun 20, 2023, 4:04 PM
Resolved it like this, but it’s not the most elegant solution. Much appreciated if someone has a better one to share.
Basically re-querying the same document twice with the conditional within the query.


*[_id in [$published, $draft]] {
  "slug": "/" + array::join(
    array::compact([
      *[language=="en" && _id in [$published, $draft]][0].slug.current,
      parent->parent->slug.current,
      parent->slug.current,
      slug.current
    ]), "/"
  )
}
Jun 20, 2023, 4:15 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?