Finding elements starting from the end of an array has gotten a lot easier with the introduction of the at
, findLast
, and findLastIndex
methods!
With at
you no longer need to remember to access the end of the array like array[array.length - 1]
trick. You can just call .at[-1]
to get the last element!
findLast
and findLastIndex
work the same as their find
and findIndex
counterparts. But, they work backwards from the end of the array. This is useful if you know what you're accessing is going to be closer to the end.
[00:00] Let's create an array with some example elements. When we want to reach an array item, counting from the beginning of the array, counting from the left hand side, there is nothing interesting. It's very simple. We're just passing what is the index within the square brackets, which is the index access. However, this notation is not really convenient if we want to [00:20] reach an element somewhere from the right hand side, from the end of the array. And that's because first, we have to grab what is the length of the array and then not to forget about subtracting the value 1 because arrays are being index from 0 and a length of the array is a positive number. So failing to [00:39] remember about this can lead to pretty simple to fix, though maybe not very simple to spot, bugs in our code base. So just to make things simpler and just not to being forced to remember about these things, JavaScript has introduced a new at method, which is certainly not the [00:59] array index access, so not the square brackets. It is just a method, which is going to work exactly the same when we're accessing the values from the beginning, from the left hand side of the array. So nothing changes here. However, if we want to reach the elements from the end, we just need to pass a negative [01:20] index. So passing minus 1 is simply going to reach the last element. Then if we want to reach one element before. So second from the last, this is going to be minus 2. And the same way, we can basically calculate whatever the index is just counting from the very end. [01:40] Let's clear the console and reintroduce the array back again for clarity. When we want to grab the first element of the array which meets a condition, we can use the array dot find method, which is going to traverse the array from the left hand side and simply return the first item that meets the condition. So let's just create an [02:00] example. So we're going to return the fairest element which is going to be bigger or equal than 9. So that's very clear what is going on here. However, if we take a look what is going on underneath, let's just introduce a console log statement inside. [02:20] We're just going to console log that there has been a check and we will see that there has been 9 checks before the first item has been found. So what JavaScript has introduced recently is the find last method, which is doing the very similar thing. [02:39] However, it's traversing not from the left hand side, but from the right hand side, which can improve the performance when we need to traverse the list. And we know that this is going to be somewhere near the end and not near the beginning. And similar, as we have the find index method, which is [02:59] again, traversing the array from the left hand side. We have an analogic method, which is finding the index from the end of the array. So we can see that instead of 9 checks, we have only one check.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!