Creating an array of numbers with _.range in lodash
1 - What to know first
1.1 - The source code examples here are on github
2 - Creating an range of numbers using lodash
2.1 - Basic lodash range example
There is not much to write about when it come to using _.range, just call the method, and pass a count of the number of numbers that you need in the array if you just want a range of numbers from zero upwards then just one argument will work just fine like so.
Most of the time when I need an array like this this is all I need to do. However there are some additional arguments for the lodash range method. Also there are some limitations with the range method that can still be addressed with other lodash methods. So with that said lets look at a few more basic examples of making a range of numbers with lodash methods.
2.2 - Start and end values
There may come a time now and then where I will want a range that will start at a number other then that of zero. Te good news with this is that this can be done with the lodash range method by using two rather than just one arguments. So then a range between a start and end value can be done by passing the starting number first, and then the ending number.
This works by a start and end value, rather than a start value and a count or size of elements from that start location. So that is one little detail where I might want some other kind of method for this sort of thing. However maybe a more important detail is being able to set a step or delta value other than one. This two can be done with the range method, but only in the from of a number value rather tan a function that can be used to create such a value. So the rest of the examples of this section will have to do with this, with just the lodash range method as well as with other lodash method that there are to work with.
2.3 - Setting a step value
So then it is also possible to use three arguments when calling the lodash range method. When three arguments are used the first is the start value, the second is the end value, and the third argument is the step rate. It should go without saying that the default steeping value is one, so when using this it would make sense to use a higher, or lower value. When using a negative number that will result in what one would expect when it comes to counting backwards.
2.4 - Using _.map to create an array of sequential numbers
So a solution for this could be done with lodash _.map or the native array map method as well by creating an array that has a length that is the desired range, and then using the index argument that is given to the callback to set the numbers.
However if you aim to use the native Array.map it will not call the function for elements that do not have values, so a solution like this will not work with the native Array.map unless the array is filled before hand some how.
2.5 - Custom map example
Using map might prove to be a better option in some cases. That is that for the most part the range method will work fine, but now and then I might need to work something else out when it comes to the stepping, or I might want to give a size argument rather than an end value. So in this section I am using the lodash map method once again to make a custom method for creating a range of numbers.
This might not be the best example, but the basic idea is there. Using the lodash map method, or some native counterpart allows for me to define what the expression is to create the numbers. It might prove to be more complex, but what the situation calls for it some times I just have to do something like this. However doing something like this also makes me question why I should bother with lodash, it would seem that there are times where what i really need to do is create some kind of custom utility library with methods that I am actually gong to use.
3 - Reversing the order of the elements
One thing that might come up is the idea of reversing the order of the elements. That is doing more or less the same thing my make it so the first element is the last number in the range. When it comes to this there is a method for this purpose itself actually as well as other lodash methods for doing this sort of task in general.
3.1 - The lodash range right method
One way to do this would be to use the range right lodash method which works just like lodash range only it will return a reversed order.
There is also the question of this method really being needed in lodash or not. After all there are methods that can be used to reverse the order.
3.2 - The lodash reverse method with the regukar ranmge method
The range right method is one of the methods in lodash that is not all that necessary. One reason why is that once can just use the regular lodash range method and just reverse the order of the elements. There is bolth a lodash reverse method, as well as a native array method that can be used to do this.
4 - Vanilla js range method examples
4.1 - Using a while loop
Yes it is not to hard at all to make a vanilla js replacement for the lodash _.range method. For this example I quickly worked out a range method that makes use of a while loop. In the body of the function I just need to create a blank empty array, and then just push numbers into the array in the body of the while loop.
Still making a simple method like this takes a little time, and if it is there in lodash all ready, and lodash is part of the projects stack, then there is no need to bother.
4.2 - Using the native array map and fill method
So then there is also using the native array map method over the lodash map method as yet another way to make a range. One drawback over the native array map method is that it will skip over empty elements. So one way to go about addressing this would be to use the array fill method as a way to just make the elements not empty. Then the array map method will work with the resulting array returned by the array fill method.
5 - Conclusion
I do get around to editing my content on lodash now and then. So at some point in the future I might get around to expanding this post more. However there might only be so much more to write about when it comes to this topic actually. So maybe the only thing to do at this post would be to make a few simple use case examples. When there is not much more to write about when it comes to the method itself, as well as the native counterparts, some times that is all that there is to do when it come to moving forward more.