The lodash _.sample method for simpler sampling
Time for another post on lodash because it is still widely used these days, and it is still useful even in light of new features added to native javaScript in late specs of the language. Regardless of what people say, methods like _.sample help to make coding in javaScript faster, and more concise. However it is still not to hard to clone many such methods with plain vanilla javaScript. In this post I will be writing about _.sample, as well as native javaScript solutions for the same task. These examples might help to show why many still like to use lodash to help get things done faster, and focus more on what really matters when working on a project.
1 - What to know
This is a post on the lodash method _.sample, and a few related methods, as well as some corresponding plain old javaScript examples as well. It is not a getting started post on lodash, or javaScript in general.
2 - Basic example of _.sample
So Sample is pretty straight forward, just give it an array, and it will return a random element from that array, a common task that comes up a lot when developing.
|
|
2.1 - Compare to a vanilla js solution.
It’s not like doing this in plain old javaScript is that hard, and if you are just using lodash for this method and nothing else, it is kind of overkill, unless you just use just this one method or some kind of equivalent.
|
|
The think about this is that if I am going to bother with lodash, rather than going pure vanilla as some might say, I would of course use more than just one method, so maybe a more advanced example that makes use of a few lodash methods is in order to really see how lodash does in fact help.
3 - The grid object example
For a more advanced example of this I made an object that represents a grid of objects. The objects are stored as a linear array, so I would want some methods that can be used to convert it to an array of arrays. For the sake of this post I would also want a method that I can use to get a random object, ans well as a random row, and col of objects.
3.1 - The lodash version of the gird object
If using lodash there is _.chunk, and .zip that are very useful when dealing with these kinds of situations apart from that of just \.sample. So for my example of the grid object using lodash I would use those methods.
|
|
I was able to put this together in a flash, and the code is very short, and clean. This is what lodash ( and making use of what is all ready out there in general ) is all about, making use of usual suspect methods to make quick work of things, and move on with what really matters.
3.2 - A vanilla js alternative
So for me it’s not so hard to make a vanilla js alternative to this, but it was still far more time consuming. When coding with vanilla js I seem to prefer while loops over for loops or for each.
|
|
There are many ways to go about crunching this down a little more I am sure, but you get the idea. I know that it is nice to have a situation in which I am not depending on additional external resources, and as such am juts working directly within javaScripot itself. However these days, as I work on more complex projects, I often do just end up making lodash part of the stack, and if it is there I might as well make use of it.