Query last 10 elements from array in Sanity GROQ

7 replies
Last updated: Jul 11, 2022
Hi,I'm trying to query an array but I want the last 10 elements of that array. Is it possible to do this one one query? This is what I'm trying to do and I get an error:

  *[_type == "archives"] {
    title,
    slug,
    links[(count(links) - 10))...count(links))]
  }
In this case
links
is an array.
AI Update

GROQ doesn't support negative indices in array slicing the way you might expect from other languages. Your syntax links[(count(links) - 10))...count(links))] has a couple of issues.

To get the last 10 elements of an array in GROQ, you have a few options:

GROQ does support negative indices! According to the GROQ pipeline components documentation, negative range endpoints are based at the end of the array. You can use:

*[_type == "archives"] {
  title,
  slug,
  "lastTenLinks": links[-10..-1]
}

This syntax [-10..-1] means "from the 10th-to-last element to the last element."

Option 2: Order and slice from the beginning

If you want to be more explicit or if negative indexing doesn't work in your GROQ version, you can reverse the array and take the first 10:

*[_type == "archives"] {
  title,
  slug,
  "lastTenLinks": links | order(@) | [0...10]
}

However, this approach may not preserve the original order.

Why your original query failed

Your query links[(count(links) - 10))...count(links))] has these problems:

  1. Extra parenthesis: You have (count(links) - 10)) with double closing parens
  2. Non-literal range: According to the GROQ documentation, "The range must be a literal range due to parser ambiguity with filters" - you can't use computed values like count(links) in the slice syntax
  3. Three dots vs two: Use .. (two dots) to include both endpoints, or ... (three dots) to exclude the right endpoint

The solution

Use the negative index approach with two dots (which includes both endpoints):

*[_type == "archives"] {
  title,
  slug,
  links[-10..-1]
}

This will gracefully handle arrays shorter than 10 elements - it'll just return whatever is available. The negative indices are based at the end of the array, so [-10..-1] gives you the last 10 items (or fewer if the array is shorter).

Show original thread
7 replies
oh I think that might have been an old screenshot when I was playing around with it
this is the error I get
lmao I thought I was going crazy, looked up a bracket validator
Unfortunately it's not possible to do calculations in a slice. For use cases like pagination we currently recommend managing your state and slices on your front end.
Unfortunate but thanks for the response.
You should be able to use negative slicing:
links[-11..-1]
. Negative offsets count from the end, with -1 being the last element.
I'm not in front a computer right now, but I
believe this works correctly if the array is less than 10 elements, i.e. the slice start is clamped to the range [0, length-1].
Oh thank you. I think for my use case, it works out well.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?