🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Next.js - How to Filter an Array of Objects for a Specific ID

2 replies
Last updated: Sep 26, 2022
Hello ! I need help of this :
I have a VanguardsList document containing a Vanguards array of vanguard object .
I set an id for every vanguard (which I use as a slug for my dynamic routing in NextJs).
inside NextJs getStaticProps(), I only want to fetch the vanguard object matching the id (the current slug of the page..).

Currently, I am able to filter de data but I still get an array containing multiples objects (all empty except the one with the right id).

I want to query only the object witht he right id (without the empty ones)

first img is document structure and second is query inside Vision
Sep 26, 2022, 9:29 PM
user N
! You man move the filter up to your array like so to only return the items with a matching id:
vanguards[@.id == 2]
Sep 26, 2022, 9:39 PM
user M
thanks ! that worked
Sep 26, 2022, 9:41 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?