I have been using the Lodash library a lot lately, then I decided to list here some functions that I consider very useful. You will notice how it can simplify your code, making it more elegant, cleaner, and functional.
Lodash, is a tool kit of JS functions that provides methods for manipulating objects and collections. It’s like a utility belt!
In order to filter collections, we can use the filter method. It’s returns only those objects that contain the specified key, and it’s also supports nested objects.
The groupBy method takes an array of objects and groups them by some field or condition.
The merge method takes multiple source objects, and merges their properties together into an object.
Selecting properties to create a new object
The pick method creates an object composed of the picked object properties.
Lodash allows us to chain the collection functions instead of using a bunch of temporary variables, making your code more readable.
Let’s say that we have an array of pets and we need:
- get all pets that are dogs
- get just the dog name
- order by name
- join the names with commas
More about Lodash
There are hundreds of useful functions available in the Lodash library. Here I just listed some simple examples, but they’re enough to realize how useful it can be for your Js projects.
If you want to learn more, Lodash website is an excellent resource. Feel free to write your questions in the comment box.
Thanks for reading. Until next time!