So maybe there are still a few things to write about when it comes to a method like the lodash first method, after all there is not just getting the first element of an array when getting the first element of an array. For example there is getting the first element of an array, and also mutating the array in place while doing so. There is trying to get the first element of an array, but getting a default value in the event that the first element of an array is undefined. There is also sorting an array before getting the first element, and when doing so mutating in place and not mutating in place.
To start out with in this section I will of course be going over a few basic examples of just the lodash first method. There is only so much to cover when it comes to using this method of course, I give it an array, it returned the first element in that array at index 0. So while I am at it I will also be writing about a few other lodash methods while using the lodash first method that address some of the draw backs of using this method alone.
So the lodash first method is an array method that can be used to just simple get the first element of an array at index zero. Just pass the array as the first argument when calling lodash first and the first element is returned.
The lodash first method will not mutate an array in place, more often that not this is a desired effect. However if for some reason I do want to not just get the first element, but also remove that first element while doing so, then I will need to use some other method. One way to address this potential draw back of the lodash first method would be to use the lodash pullAt method that will create and return a new array with removed element form a source array by way of a given index value. SO I can call pullAt give the source array, and then the index 0 for the first element. The returned result from pullAt will then be an array so once again I can then use the bracket syntax to get the value of that first element.
Another draw back of the lodash first method is that it will only take one argument, so then there is no additional arguments or options with this method, such as setting a default value in the event that the first element is undefined. Other methods in lodash will allow for this kind of feature such as the lodash get collection method.
The lodash first method only takes one argument and only returns the element that is the first element of the array that it is given. If you want an array of elements from zero forward, and you want that array sorted, then there is the slice, and sortBy methods that can be used.
So there is not much or a reason to bother with many of these lodash methods once a developer becomes used to what there is to work with when it comes to native methods. Maybe there are some talking points when it comes to using abstractions even for methods like this, but I would say it is largely a nit pick issue. The real concern is if what i am developing to begin with is something that is work my time after all.
The array sort method can be used to sort an array in place, the slice method can then be used to get a new array from that sorted array.
So the lodash first method is not one of the most compelling methods to help build a case as to you developers should continue bothering with lodash. Do not get me wrong I do not care to hop on the kick lodash to the curb band wagon just yet necessary. It is just that I have to admit that if I just want to get the first element of an array, it is not such a big deal to just use the bracket syntax with the index value of zero.
The redeeming qualities of lodash have a lot to do with the idea of what functional programing is, and the benefits that are gained by having stand alone methods rather than native ones which it comes to things like monkey patching native methods compared to just using a complete stand alone method. However all of this is a matter for another post, and with that said of you enjoyed this and would like to read more on lodash I have a lot more posts on lodash beyond this one.