Introduction to Lodash

By – Rohit Malhotra (Engineer, Marketplace)

UC Blogger
Urban Company – Engineering
4 min readMay 19, 2021

--

Lodash is a JavaScript library which provides utility functions for dealing with javascript objects and arrays, enhancing productivity and code readability.

Lodash provides a plethora of functions, following are some of them that will help in solving the most common challenges when dealing with javascript objects.

_.map

Iterates over an array or properties of an object and returns a new array with values as the result of the callback function.

The above can be achieved through es5 map as well but _.map can be used in other ways. Like getting a specific property from an array of objects.

Works with nested properties as well

Similar useful functions: _.mapValues, _.mapKeys, _.flatMap

_.find

Iterates over elements of collection, returning the first element predicate returns truthy for.

Similar useful functions: _.findIndex, _.some

_.filter

Iterates over elements of collection, returning an array of all elements predicate returns truthy for.

For example, we want to filter the array of objects based on the profile.experience property.

Similar useful functions: _.every

_.keyBy

Creates an object composed of keys generated from the results of running each element of collection through the callback function.

We can convert an array of objects into a single object having properties as the unique identifier of each object. This way we can easily access the object based on that unique identifier.

Similar useful functions: _.groupBy

_.get

Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.

Normally in javascript, to get a nested property of an object, we have to write something like the following:

Very common use case, using _.get not only makes the code more readable but also avoids annoying errors like cannot read property 'experience' of undefined .

Similar useful functions: _.set

_.cloneDeep

This method recursively clones the whole object, so if any property of the resultant object changes, it will not change the original object as the references will also be new.

Goodbye to code likeJSON.parse(JSON.stringify(obj)) .

Note: _.forEach, _.reduce, _.assign are some of the functions I skipped since they pretty much work the same as their ES5/6 counterparts. If these don’t sound familiar, you should definitely check them out.

Function Chaining

_.chain can be used to chain functions together as follows:

This way code becomes much more readable.

One downfall with _.chain is that we cannot use user-defined functions on the object returned by it. But lodash does give a way to do it by using _.mixin as shown in the following:

Conclusion

Lodash as a javascript utility library provides many useful functions that one needs to deal with arrays, numbers, objects, strings, etc.

At the time of writing, it is the most depended upon package on npm. So rest assured while using it as it is one of the most well-maintained libraries.

References: https://lodash.com/docs

--

--

UC Blogger
Urban Company – Engineering

The author of stories from inside Urban Company (owner of Engineering, Design & Culture blogs)