#How to get the collection items index?

3 messages · Page 1 of 1 (latest)

mellow jewel
#

Hi there,
Be kind, it's my first message here, and I'm a newbie to almost everything when it comes to web development.

I'm building an image gallery using Astro, and the content collections, and it works great. But I'm facing a small issue : I need to get the index of the collection items.

Any idea how to get this?

Thx.

#

How to get the collection items index?

pallid osprey
#

I guess you are looking for something like this:

`import { getCollection } from 'astro:content';
const posts = await getCollection('posts');

import Layout from '../layouts/Layout.astro';

<Layout>
{posts.map((post, index) => (
<div>Post number: {index}</div>
<div>Post title: {post.title}</div>
))}
</Layout>`