How to print a title for each value in an array of objects in Sanity schema.
Great to hear you're enjoying Sanity! Yes, you can absolutely customize how array items display their titles instead of showing the stringified object. You'll want to use the preview configuration on your array item's object type.
Here's how to do it:
{
name: 'myArray',
type: 'array',
of: [
{
type: 'object',
fields: [
{
name: 'title',
type: 'string'
},
{
name: 'description',
type: 'text'
}
// ... other fields
],
preview: {
select: {
title: 'title',
subtitle: 'description'
},
prepare({title, subtitle}) {
return {
title: title,
subtitle: subtitle
}
}
}
}
]
}The preview configuration works in two parts:
select- Specifies which fields you want to use in the previewprepare- Transforms those selected fields into what's displayed
You can also add a media property if you want an icon or image thumbnail:
preview: {
select: {
title: 'title',
media: 'image'
},
prepare({title, media}) {
return {
title: title,
subtitle: 'My custom subtitle',
media: media ?? MyDefaultIcon // Fallback icon if no image
}
}
}The preview configuration also works great for creating richer array item previews - you can show things like status indicators, dates, percentages, or any other contextual info that helps editors quickly understand what each array item contains.
If you need to access fields from referenced documents in your preview, you can use dot notation in the select: authorName: 'author.name' will pull the name field from a referenced author document.
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.