✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

How to print a title for each value in an array of objects in Sanity schema.

5 replies
Last updated: Jul 16, 2021
Hi! I am starting with Sanity and so far it is quite a nice ride.
I have a question about
array
type that I haven’t found the answer too in [docs](https://www.sanity.io/docs/array-type ). My array of objects works as expected, but I want to print a title for each value (and not show just stringified object itself). Is that possible?
Jul 16, 2021, 1:19 PM
So ideally from each item I would show
Object.values(item.title)[0]
(which is the i18ned title field, its first value which is my default language).
Jul 16, 2021, 1:20 PM
This is the relevant part of my schema:

{
      name: "list",
      title: "Features",
      type: "array",
      of: [
        {
          title: "Quick features",
          type: "object",
          fields: [
            {
              name: "title",
              title: "Title",
              type: "localeString"
            },
            {
              name: "icon",
              title: "Icon",
              type: "object",
              fields: [
                {
                  name: "Icon",
                  type: "string",
                  layout: "dropdown",
                  options: {
                    list: getIcons()
                  }
                }
              ]
            },
            {
              name: "description",
              title: "Description",
              type: "localeString"
            }
          ]
        }
      ]
    },
localeString is object (czech and english string field)
Jul 16, 2021, 1:20 PM
Also unfortunately I’m getting a value
null
in FE for this field. 😞 Am I doing wrong something obvious?
Jul 16, 2021, 1:57 PM
Hi Petr. You can show the correct field using a preview . It’s a bit of a different syntax, but I expect you can print the title with something like this, which should be a sibling of your document’s
fields[]
array:

  preview: {
    select: {
      title: 'list.0.title.en',
    }
  }
It might need some adjusting based on what
localeString
looks like.
As for the null value on your front end, I would log out your query result to see exactly what your data structure looks like, then drill down or destructure from there.
Jul 16, 2021, 2:32 PM
Perfect, thanks!
Jul 16, 2021, 2:33 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?