Increase you productivity with Lodash

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.

  1. Using CDN.
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
  2. 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.

  1. Get
    This for me is the most useful function provided by Lodash. This function gets the value of the provided key in the object . If the value is undefined ,i.e, the key is not present in the object, it returns the default 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"
    
    
  2. isEqual
    This method performs a deep comparison of the obj1 and obj2 and returns a boolean.

    const ob1 = {
    "a":"apple",
    "b": "ball",
    }
    
    const ob2 = {
    "a":"apple",
    "b":"ball",
    }
    
    result = _.isEqual(ob1,ob2)  //true

     

  3. isEmpty
    Checks if the object is empty or not and returns a boolean.

    const ob1 = {
    
    }
    
    result = _.isEmpty(ob1) . //true
    
    

    You can also use to check for the empty object, but isEmpty makes it easy to perform the check.

  4. 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"
       }
    ]
    
    
    
  5. orderBy
    OrderBy function is similar to sortBy function, but it also allows you to sort by ascending or descending. For descending order desc is used, for ascending asc 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 sorted ob1 in ascending order of alphabets and then descending order of word 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"
       }
    ]
    
    
  6. 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 a timeout function gets. This is direct mitigation. It will call the func function after delay 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.

3 thoughts on “Increase you productivity with Lodash

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)