👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

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 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?