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.

About the Author

Object Partners profile.
Leave a Reply

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

Related Blog Posts
Why we started using JSON Schema in our internal Helm charts
Helm 3 supports validating the provided chart values against a JSON Schema. While it may be quicker to get started in your chart development without a schema, we found it valuable for a number of […]
Rewriting files in Google Cloud Storage
Rewriting Files in GCP Note: even though this code is in Python, this should be the same idea in JavaScript, Go, etc. I wrote the following to copy a file from one Google Cloud Storage […]
Interpreting Spatial Data in the Age of COVID-19
As 2020 has come to an end, many are eager to leave the mess of COVID-19 behind with the new year and gain a fresh start. Unfortunately, new cases are still soaring across the United […]
Building a Better Mousetrap
Recently, my daughter (age 6) was into building “mousetraps” out of shoe boxes. These were more or less comfortable cardboard mouse houses complete with beds, rooms, everything a mouse could want or need and not […]