👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
For a long time, when writing different functions, we may want to create ranges to work with certain data. A range represents data in an array or object with a start and end value.
Many languages ​​have built-in methods for creating ranges, for example to_a
in Ruby:
JavaScript doesn’t have a specific built-in method for it, but there is many ways to create ranges. We will show you three of them.
_.range () - Lodash
If we want to create a range of numbers and we want them faster possible, we can use the Lodash _.range ()
util method.
This method returns an array and accepts three arguments: the first for the start of the interval, the second for the end of the interval (up) and the third for no or increased / decreased value by.
Note that if only one argument is given, it will create a range up to the number specified. Also, don’t forget that Lodash is a JavaScript library that we have to install separately!
Create our function range () .
If we want to save overhead, we can create our own number range function. It turns out that creating our range is pretty straightforward.
It’s easy to write and you can avoid importing libraries like Lodash for a single function!