Modifying the first item in an array using GROQ query in Slack thread.

14 replies
Last updated: Aug 31, 2022
I’m grabbing all the chapters in a module, but I’d like to modify only the first item in the resulting array. is that possible in the GROQ query?

chapters[]->{ title, _id, slug, _type }

Aug 30, 2022, 1:05 PM
I think if I could get the current index, I could use a conditional statement, but I’m not sure if that’s possible
Aug 30, 2022, 1:13 PM
That's a great question. I could only make it happen by giving it its own slot, like

"firstChapter" : chapters[0]->title,
"others" : chapters[1..99]->title
What sort of modification are you looking to perform? Is it changing the nature of what's returned or is it transforming the data in such a way as to maybe delegate it to the front end?
Aug 30, 2022, 1:45 PM
I’m aiming to replace the slug/path of the first item in the list so that the links to the module show the content of the first item
Aug 30, 2022, 3:12 PM
I’m currently doing the transformation on the front-end, it’s just in a few places, so I need to rewrite it into a reusable function, but figured if I could lift it up to the data query it would be easiest to manage all in one place
Aug 30, 2022, 3:13 PM
The way you did it is the only way I could figure too. There doesn’t appear to be a function to get the index of the current array item
Aug 30, 2022, 3:13 PM
There doesn’t appear to be a function to get the index of the current array item.
This is correct.
Aug 30, 2022, 3:14 PM
user A
Aug 30, 2022, 3:25 PM
user J
You can't retrieve it, per se, but you can see if it matches, I think.
This is maybe silly but it worked for me in testing if you wanted a "token"; I think it could only work for a known "index" but here it is:

*[_type=="news"][0]._id == @._id => {
      "isFirst" : true
It evaluates a condition that just says, "if you grab all the news articles and the first result's ID matches this document's ID, add this object because it must be the first one. I tested it with 4 for fourth, etc. and it was consistent. Could be worth a shot!
Aug 30, 2022, 3:52 PM
Array items aren’t returned by the sort order in the Studio, so unless I’m misinterpreting the question, this wouldn’t return the correct item (I could be wrong though, and please correct me if so).
Generally, plugins like Simeon’s orderable document list plugin will store the order on the document itself, which
can be read in GROQ. I think you could use a document action to write to your “first” array item upon edit/save.
Aug 30, 2022, 7:03 PM
Ah, yeah, I wasn't thinking in terms of fields. My thinking was broadly that the same request would (or should) always get the same result, whatever the request happens to be, and that we could simply to feed the same query back to itself irrespective of what the manner in which it created the order (or whichever one someone chose in the GROQ request).
I am not sure of his schema or setup so I was making assumptions using my imagination =P
Aug 30, 2022, 8:14 PM
Array items aren’t returned by the sort order in the Studio
I didn’t realize that, they always seem to come in the order of the list shown in the studio
Aug 31, 2022, 11:53 AM
Thanks for the help
user S
user A
, I was able to get it to work like this,

^.section[0]->._id == @._id => {
  "name": ^.slug.current,
  "title": ^.title,
  "path": "/" + ^.^.slug.current + "/" + ^.slug.current + "/"
So inside the section[] loop, I look up to the parent, get the first section and populate the reference to get the id, then compare to the current id. If it matches, I overwrite the necessary attributes.
Aug 31, 2022, 5:56 PM
Nice! Glad to hear it. 😀
Aug 31, 2022, 6:00 PM
Oh wow, I am so used to having the left-hand side being a string I didn't think of loading a conditional from the get-go by the parent. Smooth moves,
user J
Aug 31, 2022, 6:20 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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?