The lodash last method is an array method that can be used to get the last element in an array. On thing about the lodash last method is that this method will not mutate the array that is given, so this makes the lodash last method a fairly simple example of a functional programing style pure function. However this might not be the best example of this talking point as to why devlopers should use lodash, or some other functional programing utility library.
The lodash last method works by just calling the method, and then passing the array to which I want the last element. When doing so it will give the last element in the array without removing it from the given source array.
So there is also the lodash _.remove method that can be used to get the last element of an array. However it is a far more complex solution for something that is fairly simple. The lodash remove method works by passing the array as the first argument and then a function that will be called for each element. In the body of the function that is called I could give an expression that will return true when it is the last index in the array.
It works sure, but for something so simple it is a bit much. The lodash remove method should be used when there is some far more complex set of criteria that is required when it comes to removing and returning the last element in an array.
So if I want to just get the last element in the array, and not remove it. There there is just using the Array length property minus one as a way to get the zero relative index value of the last element like so.
So then there is the Array pop method. This array prototype method will return the last element in an array, and it will also remove it at the same time.
Of course there is also Array.splice that can be used to return a a new array that is a slice of the array from which it is used. So then this can be used as an alternative that works just like lodash last as well sense it does not remove the last element just gives it to you.
The array slice method works in a similar way to that of slice, but index values must be given for both arguments. In addition this will also mutate the array from which it is called.
Well there you have it, the lodash last method is to just get the last element in the array. So there is really not much more to say about it beyond additional ways of doing so with and without lodash. I do not use lodash that much these days, and when I do it is often to just use one or two methods like that of merge, or chunk. I can nit say that the lodash last method is a great talking point as to why we should keep using lodash, but I suppose it does still have its redeeming qualities.