Getting the index of the last matching item in an array with vanilla JS

Let’s say you have an an array with a list of awesome wizards.
let wizards = [‘Gandalf’, ‘Merlin’, ‘Radagast’, ‘Merlin’]; You’ll notice that Merlin appears in the list twice. You can get the index of the first instance of Merlin using the Array.indexOf() method.
// returns 1 let index = wizards.indexOf(‘Merlin’); But what if you wanted to get the index of the last instance of Merlin in the wizards array instead?


This content originally appeared on Go Make Things and was authored by Go Make Things

Let’s say you have an an array with a list of awesome wizards.

let wizards = ['Gandalf', 'Merlin', 'Radagast', 'Merlin'];

You’ll notice that Merlin appears in the list twice. You can get the index of the first instance of Merlin using the Array.indexOf() method.

// returns 1
let index = wizards.indexOf('Merlin');

But what if you wanted to get the index of the last instance of Merlin in the wizards array instead? For that, we can use the Array.lastIndexOf() method.

Just like with Array.indexOf(), pass in the item to get as an argument. It returns the index of the last instance of that item, or -1 if it doesn’t exist.

// returns 3
let lastIndex = wizards.lastIndexOf('Merlin');

If there’s only one instance of an item, it still works.

// returns 0
let gandalf = wizards.lastIndexOf('Gandalf');

Here’s a demo.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2021-06-15T14:30:00+00:00) Getting the index of the last matching item in an array with vanilla JS. Retrieved from https://www.scien.cx/2021/06/15/getting-the-index-of-the-last-matching-item-in-an-array-with-vanilla-js/

MLA
" » Getting the index of the last matching item in an array with vanilla JS." Go Make Things | Sciencx - Tuesday June 15, 2021, https://www.scien.cx/2021/06/15/getting-the-index-of-the-last-matching-item-in-an-array-with-vanilla-js/
HARVARD
Go Make Things | Sciencx Tuesday June 15, 2021 » Getting the index of the last matching item in an array with vanilla JS., viewed ,<https://www.scien.cx/2021/06/15/getting-the-index-of-the-last-matching-item-in-an-array-with-vanilla-js/>
VANCOUVER
Go Make Things | Sciencx - » Getting the index of the last matching item in an array with vanilla JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/15/getting-the-index-of-the-last-matching-item-in-an-array-with-vanilla-js/
CHICAGO
" » Getting the index of the last matching item in an array with vanilla JS." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2021/06/15/getting-the-index-of-the-last-matching-item-in-an-array-with-vanilla-js/
IEEE
" » Getting the index of the last matching item in an array with vanilla JS." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2021/06/15/getting-the-index-of-the-last-matching-item-in-an-array-with-vanilla-js/. [Accessed: ]
rf:citation
» Getting the index of the last matching item in an array with vanilla JS | Go Make Things | Sciencx | https://www.scien.cx/2021/06/15/getting-the-index-of-the-last-matching-item-in-an-array-with-vanilla-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.