First selector

Learn how to get the first items of a list.

The first selector provides a convenient way to access the first item of a list.

Here is a basic example:

let transport = ["🚀", "🚗", "🚆"];
transport[:first] // 🚀
Try in Playground

You can also use it to access a specific number of items from the beginning of a list:

transport[:first(/*<count>*/)]

For example, to access the first two items of a list, you can use:

transport[:first(2)] // ["🚀", "🚗"]

In the real world, you can use it to access the first item a user added to their shopping cart, for example:

cart.items[:first]

And you can also use it to get the first items of a map:

let beings = ["aliens": "👽", "robots": "🤖", "humans": "👨‍🚀"];
beings[:first] // 👽
Try in Playground

This works because the :first selector does not use the index or key to find the first element, but the order in which they appear in the collection.