Last selector

Learn how to get the last items of a list.

The last selector is the counterpart of the first selector. It allows you to access the last elements of a collection without knowing the index or key.

For example, to access the last item in a list, you can use:

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

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

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

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

transport[:last(2)] // ["🚗", "🚆"]

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

cart.items[:last]

And you can also use it to access the last element of a map:

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

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