“A little boy holding a book with a surprised expression on his face” by Ben White on Unsplash

10 Lodash functions everyone should know

What is Lodash?

Lodash is a superset of Underscore. What is Underscore? Underscore is “[…] a whole mess of useful functional programming helpers without extending any built-in objects.” In short, Lodash is a JS helper library for arrays, strings and objects. Documentation is here: https://lodash.com/docs

Why use Lodash?

It will help you deal with all types of object and you will save you time by not coding generic functions. Your code will also be cleaner with less lines and it will work on all browsers. If you’re not currently using it, you should seriously think about it.

1. _.assign

_.assign is the equivalent of the spread operator from ES6. It’s pretty easy to understand, it assigns properties of one or many objects to a source object.

2. _.times

_.times receives as arguments the number of iterations and a function to execute n times and returns an array of the results. Very useful when creating dynamic test data.

3. _.debounce

_.debounce will invoke a function after a certain amount of time since the last time it was invoked.

In this example, the function validateEmail will be invoked after 500ms so the error message won’t show instantly. The 500ms timer will reset on each keyup. This way, the user won’t see an error message until he stops typing.

4. _.find

Instead iterating through an array with a loop to find a specific object, we can simply use _.find. That’s nice, but this is not the only thing you can do with _.find. You can also find an object using multiple properties with a single line of code. Take a look!

5. _.get and ._set

For this one I will cheat a little bit by presenting 2 functions that do almost the same thing. ._get will return a property value from an object and ._set will, well you guessed it, set a property with a value. Nothing special except that you can access a property with its path.
Let’s see an example.

When using _.set, if the path doesn’t exist, it will be created. No more “Cannot set property ‘items’ of undefined” error. With _.get, if the path doesn’t exist, it will return undefined instead of an error. You can also specify a default value (third argument) if the path resolve to undefined.

6. _.deburr

This one is pretty simple. It removes all “combining diacritical marks”, so “é” becomes “e”.

It’s a good habit to deburr text for a search function when there is internationalization and localization.

7. _.keyBy

_.keyBy is one of my favorites. It helps a lot when trying to get an object with a specific property. Let’s say we have 100 blog posts and we want to get the post with Id “34abc”. How can we achieve this? Let’s see!

Anytime a server returns an object collection as an array, this function can help you organize it. The second argument can also be a function.

8. _.reduce

_.reduce is a little bit like a filter function. The only difference is that you can choose the form of the returned object. If you don’t understand what I’m trying to say, it’s normal, there is an example for this reason.

Ouch! This is a tough one. So basically, we return a new object with users grouped by their age and only for user between 18 and 59. This helper function is one of the most used ones from Lodash. It is also part of ES6. I also want to point 2 common errors, don’t forget to return the result at the end of the function and don’t forget to specify the default value for the result as the third argument (here {}).

9. _.cloneDeep

After the hardest one, the easiest one. _.cloneDeep will clone an object. No kidding! The new object will also have a new address in memory so you won’t crush a property from the original object.

10 _. sortedUniq

With this one, all duplicated values won’t be returned. This is usually used for performance reasons, because it is specifically for the sorted arrays.

This one is useful only if you deal with big array. If you want better performance you should sort your array and using functions that works better with sorted arrays. There are several other functions like this with Lodash. You can take a look at _.sortedIndex, _.sortedIndexBy, _.sortedIndexOf, _.sortedLastIndex, _.sortedLastIndexBy, _.sortedLastIndexOf, _.sortedUniq, _.sortedUniqBy.


Lodash provides a ton of helper functions to help you save time. The library is very light (69 KB), efficient, popular and loved (more than 18,000 starts on Github). It is updated very often and last time I looked at their Github page, there was 0 issue (Github page). If you are not currently using it it’s very easy to incorporate in an existing project. If you’re starting a new project soon, you should really think about using it. If you are here because you don’t now if you should use it or not … well, yes you should. I will conclude with an answer given by the author on Stackoverflow:

Like what you read? Give Vincent D'amour a round of applause.
