Logo preloader Xtension

Is it worth using lodash?

Few years ago I admired versatility and simplicity of lodash library. At that time, it was a remedy for all common ills experienced by probably every JavaScript programmer. In this post I would like to share my personal observations and thoughts about it.
javascript
coding

The lodash library implements the philosophy preached by jQuery, i.e. write less, do more. It reduces common problems to single instructions. It is very popular all the time. When we go to main page of npm package manager we can find lodash on the top of dependent packages list. An additional advantage is its modularity. We can include only the components we need in our project, thus reducing the size.

 

As a key example of the use of this library I will give the need to operate on collections. We will use for this purpose first of all functions: each, filter, map, find etc. In combination with the use of "arrow functions" we get a really neat code. This is, of course, my subjective feeling, especially for someone who previously delighted with LINQ in C#.

 

It's also worth mentioning that the inspiration for lodash is another, no less popular underscore library, which performs the same tasks as lodash, but the creators of the latter brag about the consistency of behavior of some functions - it was about mutation (or lack thereof) of objects on which we call these functions.

 

I will admit honestly: until recently I thought that including this library in every, even the smallest project is something obvious. I've recently noticed (you may start laughing here) that some of these functions are available in pure JS and you don't need another library for that. I'm thinking here mainly of methods from Array.prototype. Despite this lodash provides many functions, which are not yet found in the standard. I will briefly describe a few of them.

get

This is a very useful function when you want to access some nested property of an object, and you don't want to check at every level whether the object is null (or generally converting an object to boolean results in false).

var fragmentCar = { 'wheel': [{ 'tyre': { 'diameter': 3 } }] };

_. get(fragmentCar, 'wheel.tyre.diameter', 5);

In this situation, we do not have to wonder whether the car has a wheel, and the wheel in turn whether it has a tyre, and finally whether it has a defined diameter. If any element is missing on the way, then we get a default value of 5.

range

Useful when you want to generate an array of numbers with a given range.

clone

It performs a shallow cloning of an object. There is also a version with deep cloneDeep cloning.

assign

Properties from the other object are added to one object. If there are already some properties in the target object that are present in the source object, then they will be overwritten.

flatten

It flattens an array of arrays, but only to one level. There is also a version of flattenDeep that does this recursively to the end.

_.flatten(['Ala', ['ma'], 'cat']);

The result is: ['Ala', 'ma', 'cat'].

fromPairs

An excellent function for converting an array of arrays into an object. On input you are given an array whose elements are two-element arrays, of which the first element is the key of the object to be created and the second is the value of that key.

_. fromPairs([['length', 5], ['width', 2.5]]);

The result is:{length: 5, width: 2.5}. There is also an inverse function: toPairs.

sortBy

From now on, sorting could not be simpler. As an argument, in addition to the array of course, we give the sorting method, which can be defined as

function that returns a primitive value, after which it is easy to sort 
or 

an array of property names to be sorted by.
Of course, using this function makes sense when you want to sort an array of objects. Then the greatest brevity is achieved by this second method. For example, if we want to sort the wheels by the radius of the tyre, we write:

_. sortBy(arrayWheels, ['tyre.diameter']);

groupBy

 

Another function to simplify the code. The call is similar to sortBy with the difference that now we indicate the grouping method. As a result we will get an object with keys representing the designated groups, and the values of these keys will be arrays of objects assigned to the given group.

 

As I mentioned at the beginning, there are also a handful of functions that are currently available "for free" in pure JS that operate on arrays. These include: each, map, filter, reduce. The lodash versions, however, have the advantage that they also work on "null" collections. E.g. call_. filter(null, e => true)will result in an empty array.

 

To sum up

The lodash library offers really a lot of interesting features. I have mentioned only few of them. Documentation is written clearly. Each function is illustrated with an example. Functions are grouped into categories depending on their use. I think that everyone will find there a set of functions, which will make coding easier. And despite the fact that the possibilities of JS language gradually increase, the use of lodash is still justified.

Publication date: 24 August 2018
Author: Michał Gierwatowski
Read the next articles
Mobile
Certum
Security
Biometric signature - the future of business
Mobile
Certum
Security
Biometrics and mobile security
Mobile
Certum
Security
Water-Scrum-Fall. Initiation or maturity?
Let's create a unique project together
Write to us

Xtension Sp z o.o.

ul. Opacka 12

80-338 Gdańsk

 

office@xtension.pl

 

LinkedIn