How to append an array in GROQ?
Yes, exactly! In GROQ, you can use array splatting (also called the spread operator) to concatenate arrays. The syntax [...a, ...b] will combine arrays a and b into a single array, and those can be GROQ queries.
Here's a practical example from the Sanity community:
"items": [
*[_type == "groupsPage"][0] {
"title": "Overview",
"url": "/aag-ugsg/"
},
...(*[_type == "groupsPages"] {
title,
"url": "/aag-ugsg/" + slug.current
})
]In this example, the query creates an array that starts with a single transformed document, then spreads (concatenates) the results of another query that returns multiple documents. The three dots (...) before the query unwrap the array returned by that query and merge its elements into the parent array.
This is particularly useful when you need to:
- Combine results from different queries
- Prepend or append specific items to query results
- Build custom ordered lists from multiple sources
The spread operator works with any array expression in GROQ, making it a flexible tool for composing complex data structures from multiple queries. Note that you'll typically want to wrap the query in parentheses when using the spread operator to ensure proper evaluation order.
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.