We gain from everything in our lives and this learning proceed until we live. I have joined this fellowship when I was feeling much negative vitality from my workplace, lower in energy, like have no…
In this Vue.js tutorial, I’ll show you how to get the last item of an array using the slice()
method.
You can use the slice(-1)
method, which returns a part of an array, without modifying the original array. Which makes it better than using the pop()
method, which changes the length of the original array.
Suppose you have an array of items: YouTube, Spotify, Facebook and Snapchat. If you want to get the last item (Snapchat) and do something with it (e.g. delete it), you can use the Array.prototype.slice() method.
HTML:
Output:
Items:
YouTube
Spotify
Facebook
Snapchat
Last item:
Snapchat
You can also use the slice()
method with an array of objects.
We get the last project using the slice()
method in the computed property lastItem()
and then display it in the template.
HTML:
Output:
Projects:
Project S (inactive)
Project E (inactive)
Project Y (active)
Project X (active)
If you find this post useful, please let me know in the comments below.
Cheers,
Renat Galyamov
As a firm believer in the power of women, I curated, researched and shot original stories from the city of Pune, where I served as the Features Editor. I created a series of travel videos for a…
Accion Nacional se ha formado para una tarea, la mas levantada y la mas urgente en México, que es la de despertar la conciencia cívica, hacer ciudadanos, buenos ciudadanos. La ciudad, la nación, son…
He stopped playing violin because he had no one to play for and didn’t see any purpose in playing in the first place. They simply do things because they want to and know they must be done, and then…