Groovy convenience in JavaScript with Lo-Dash

Some of the nice benefits of using Groovy over Java are the convenience methods that Groovy adds to standard Java classes. Methods such as .find(), .each(), and .collect() that Groovy adds to Java’s collection classes are some of these handy shortcuts. These methods are great for backend code written in Groovy, but what if we had something similar for JavaScript on the frontend?

The Lo-Dash Javascript library is a collection of functions that provide exactly what we’re looking for. Below we’ll go through several of Groovy’s convenience methods and their corresponding Lo-Dash equivalents.

Groovy methods and their corresponding Lo-Dash equivalents

.collect() -> _.map()

One convenient and powerful collection method in Groovy is .collect(), which we can use a function to transform all elements in a collection. Lo-Dash has a similar function _.map():

Groovy:

Lo-Dash:

Note: Lo-Dash sometimes has additional methods that are effectively aliases of a main method, and _.map() is one of these methods that has an alias. You can also use the _.collect() alias instead of you prefer.

.find() -> _.find()

With find() in Groovy and in Lo-Dash we can find the first element in a collection that matches a given matcher function. For example, if we wanted to find the first element in a list that was an even number:

Groovy:

Lo-Dash:

And if instead of finding the first element that matches the function you want to find the last element that matches, Lo-Dash has _.findLast() to do just that.

.findAll() -> _.filter()

Groovy’s .findAll() and Lo-Dash’s _.filter() are similar to find(), but instead of only returning the first element in a collection that matches a function, these methods will return all elements that match:

Groovy:

Lo-Dash:

.each() -> _.forEach()

Groovy also has a handy way to run a function on each element in a collection with an each() loop, and Lo-Dash has equivalent capabilities with its _.forEach() function:

Groovy:

Lo-Dash:

.sort() -> _.sortBy()

Similar to Groovy’s sort() method, we have a couple different options for how we want to sort our collecion. We can sort collections of simple items such as numbers by just passing the collection as a single parameter:

Or if we have a list of objects that share a common field, we can sort by that field:

And if we need the most flexibility, we can use a function to calculate the sort value:

One thing to note: _.sortBy() always sorting is ascending order, but if you need descending order you can just call the Javascript .reverse() method on list after sorting it in ascending order.

.max() -> _.max()
.min() -> _.min()

Want to find the maximum or minimum number value of a list or array? Groovy has handy .max() and .min() method that you can call on the collection, and Lo-Dash has the same.

Groovy:

Lo-Dash:

Additional Resources

Lo-Dash is a very handy Javascript library that gives us some of the convenience of Groovy on the frontend. And we’ve only covered a portion of the functions Lo-Dash provides – check out their excellent documentation for all the functions in Lo-Dash and additional examples for each one.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*