Transforming array of objects into named object in GROQ query

2 replies
Last updated: Feb 29, 2024
Hey folks, GROQ question
My document has an array of objects

hours: [
  { 
    day: "monday",
    from: "9:00AM",
    to: "5:00PM"
  },
  ...
]
I'm trying to transform those days with a GROQ query into a named object:

hours: {
  monday: {
    from: "9:00AM",
    to: "5:00PM"
  },
  ...
}
Is that something that's reasonable to do in the query? Or should I just reduce that manually client-side?
Feb 29, 2024, 9:21 PM
You unfortunately can’t do this with GROQ. It’ll have to happen client-side.
Feb 29, 2024, 9:36 PM
okay makes sense, thanks!
Feb 29, 2024, 9:36 PM

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?