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
An Exploration in Rust: Musings From a Java/C++ Developer
Why Rust? It’s fast (runtime performance) It’s small (binary size) It’s safe (no memory leaks) It’s modern (build system, language features, etc) When Is It Worth It? Embedded systems (where it is implied that interpreted […]
Getting Started with CSS Container Queries
For as long as I’ve been working full-time on the front-end, I’ve heard about the promise of container queries and their potential to solve the majority of our responsive web design needs. And, for as […]
Simple improvements to making decisions in teams
Software development teams need to make a lot of decisions. Functional requirements, non-functional requirements, user experience, API contracts, tech stack, architecture, database schemas, cloud providers, deployment strategy, test strategy, security, and the list goes on. […]
JavaScript Bundle Optimization – Polyfills
If you are lucky enough to only support a small subset of browsers (for example, you are targeting a controlled set of users), feel free to move along. However, if your website is open to […]