Get a peek at our latest innovations at Sanity Product Day on Dec 8th →

Finding Total Elements in Array Property

2 replies
Last updated: Apr 8, 2021

Hi,Can someone help me with finding total elements in array property? If I have a dataset like this

{ 
  author: "Mike",
  books: [1, 2, 3]
},
{ 
  author: "Kira",
  books: [4, 5, 6, 7]
},
How can I get the total books? so the result like
totalBooks: 7
.I try
count(*[].books)
but the result is two.Thanks.

Apr 8, 2021, 1:02 AM

With API version 2021-03-25 you should be able to do this:

count(*[].books[])
. The final
[]
at the end there causes the books-array to be flattened.

Apr 8, 2021, 10:47 AM

Ah...sweet. Thank you very much Magnus.

Apr 8, 2021, 11:26 AM

Sanity.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Free to get started, and pay-as-you-go on all plans. Find out more.