Sometimes it seems like I come across something like the _.drop method in lodash, and scratch my head wondering if looking into lodash is a wise investment of time or not. A lot of these methods do work differently, and provide a certain something that does go beyond what may be in the core of js itself, so yes maybe it is worth it. However I do not thing that the lodash drop method might be the best example of this. In any case in this post I will be doing over some quick examples of the lodash drop method, as well as some plain old vanilla js alternatives.
Its a quick way to create a new array in which one or more elements are dropped from the beginning of the array. Becuase it creates a new array rather than mutating the source array that is given it makes it a kind of functional programing method example sense the original array is not mutated in any way.
It returns the first element from the beginning of the Array, and it also directly modifies the Array that it is invoked on resulting in an array that is now one element shorter each time it is called. So although it can be used as a way to remove one element from the beginning of an array also it does work a little differently.
So there is also the core js Array.slice, and splice methods that can be used to drop elements from the beging of an array, of any part of it actually. Both methods do the same thing only one directly modifies an array as Array.splice does, and the other returns a copy which is the case with Array.slice. Also they return arrays, rather than just a single element as is the case with Array.shift. However it is not to hard to just adjust for that so lets look at some examples.
Array.splice in action:
I often get the two mixed up, they both do the same thing but with one very big difference. Array.slice is the method that is more functional like because it does not mangle the original array that is given to it, so often I find myself using that one when it comes to creating a new array with some elements missing from the start, end, or any part for that matter. If I really want to though I could make my own drop method using array slice if I think that doing so is called for when it comes down to it too.
Another option is to make my own drop method with a while loop. This makes doing so a little more complicated then it needs to be but it is yet another option that comes to mind when it comes to doing this.
So then the lodash drop method is one such method where if I want to it is really not all that hard to just make my own stand alone method that does the same thing.
So the lodash drop method is a way to create a new array with the first few elements dropped from the beginning of the array, and the Array.slice method is another method that does more or less the same thing.