Being a software developer, you have to write certain utility functions time and again for the same project. We have already discussed a lot of JavaScript tricks here . In this article, we discuss Lodash. Lodash is one of the most used JavaScript library that provides a lot of utility methods that eases the task of a developer. These methods, though very simple and trivial, save a lot of time for the developer. Read this article and get to know how can you increase your productivity with lodash.
Installing Lodash:
There are a couple of ways of installing Lodash.
- Using CDN.
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
- Using NPM.
npm install --save lodash
Lodash Functions:
Now that we have Lodash installed and running, let us look at some of the methods it provides.
- Get
This for me is the most useful function provided by Lodash. This function gets the value of the providedkey
in theobject
. If the value isundefined
,i.e, the key is not present in the object, it returns thedefault value
._.get(object, key, value) const ob = { "a":"apple", "b":"ball", "c":"cat", } result = _.get(ob,"a","asd") //"apple" result = _.get(ob,"d","asd") //"asd"
- isEqual
This method performs a deep comparison of theobj1
andobj2
and returns a boolean.const ob1 = { "a":"apple", "b": "ball", } const ob2 = { "a":"apple", "b":"ball", } result = _.isEqual(ob1,ob2) //true
- isEmpty
Checks if theobject
is empty or not and returns a boolean.const ob1 = { } result = _.isEmpty(ob1) . //true
You can also use
Object.keys(obj1).length === 0
to check for the empty object, but isEmpty makes it easy to perform the check. - sortBy
The sortBy method creates an array of objects, sorted in the ascending order, by the result of a function performed on each iteratee. The function is the only piece of logic you need to inject in the function._.sortBy(array, [iteratees=[_.identity]]) const ob1 = [ { 'word': 'apple'}, { 'word': 'boy',}, { 'word': 'cats',}, ]; result = _.sortBy(ob1, [function (o) { return o.word.length;}]); //Output
[ { "word": "boy" }, { "word": "cats" }, { "word": "apple" } ]
- orderBy
OrderBy function is similar to sortBy function, but it also allows you to sort by ascending or descending. For descending orderdesc
is used, for ascendingasc
is used. Again in this function, the only piece of logic that you need to inject is the comparison function. And don’t take it lightly, you can use this function to solve complex issues. In this example below, I have first sortedob1
in ascending order ofalphabets
and then descending order ofword
length._.orderBy(object, [iteratees=[_.identity]], [orders]) const ob1 = [ { 'alphabet': 'a', "word":"apple"}, { 'alphabet': 'b', "word":"boy"}, { 'alphabet': 'a',"word":"ape"}, { 'alphabet': 'b',"word":"brother"}, ]; result = _.orderBy(ob1, ['alphabet', function(a) {return a.word.length}], ['asc', 'desc']) //Output
[ { "alphabet": "a", "word": "apple" }, { "alphabet": "a", "word": "ape" }, { "alphabet": "b", "word": "brother" }, { "alphabet": "b", "word": "boy" } ]
- debounce
This is the most fascinating function of the lot, it allows us to call the function after a delay. We all know how clumsy atimeout
function gets. This is direct mitigation. It will call thefunc
function afterdelay
delay._.debounce(func, [delay], [options={}])
These functions don’t seem to be very appealing and out of ordinary to begin with, but if you have ever worked on some large scale projects or have deadline issues, you know these are nothing short of a lifesaver. Lodash has many such utility functions and I advise the reader to give them a shot. Lodash is a library to be involved in every developer’s kitty. You can certainly increase your productivity with Lodash.
Will shortly come back with more such articles. Till then please explore libraries like this.
0 Comments