In today’s post we will analyze the advantages of using Lodash Libraries, a modern Library of JavaScript utilities that is currently giving us a lot to talk about. Let’s take a look!

What is a Lodash Library?

Lodash is a reference library made with JavaScript. In addition, it’s the most famous successor of underscore.js.

It is used for simplifying the handling and edition of objects, arrangements, etc. since this provides many useful methods to do so. At the same time, simplifying this work achieving that our code be much legible and easy to follow for third parties.

As we said previously,  Lodash is the most famous successor of underscore.js. At first it was a simple “fork” and more features were added.

To this day it has been completely redesigned.

You have the information in GitHub over here.

You have all the documentation over here.

Installation of Lodash

  • Web Navigator
  • NPM
npm install --save lodash
  • Use in NodeJS (In our Node file, we charge the component)
const _ = require('lodash')

Traditional way vs Lodash

Then, we are going to take a look of some examples about how certain functions are executed with Lodash.

  1. Return the value of a key of the first item of an array that is the value of another key:
    // Get the name of the first brother of each person
    var persons = [{
        "name": "Jose Luis",
        "brothers": [{"name":"David"}, {"name": "Sonia"}]
    }, {
        "name": "Alfonso Manuel",
        "brothers": [{"name":"Gustavo"}, {"name": "Juan"}]
    }];
    
    // Array's map method.
    var names = persons.map(function(item){
       return item.brothers[0].name;
    });
    // [ 'David', 'Gustavo' ]
    
    // Lodash
    var namesBis = _.map(persons, 'brothers[0].name');
    // [ 'David', 'Gustavo' ]
  2. Get random number between two numbers:
    // Get a random number between 15 and 20.
    
    // Native Function
    function getRandomNumber(min, max){
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    
    var random = getRandomNumber(15, 20);
    // ej: 16
    
    // Lodash
    var randomTwo = _.random(15, 20);
    // ej: 19
  3. Extend object:
    // Adding extend function to Object.prototype
    Object.prototype.extend = function(obj) {
        for (var i in obj) {
            if (obj.hasOwnProperty(i)) {
                this[i] = obj[i];
            }
        }
    };
    
    var objA = {"name": "Jose Luis", "car": "BMW"};
    var objB = {"name": "Alfonso", "age": 24};
    
    objA.extend(objB);
    // {"name": "Alfonso", "age": 24, "car": "BMW"}
    
    // Lodash
    _.assign(objA, objB);
    // {"name": "Alfonso", "age": 24, "car": "BMW"}
  4. Get last element:
    var persons = [{
        "name": "Jose Luis"
    }, {
        "name": "Alfonso Manuel"
    }, {
    	"name": "Domingo"
    }];
    
    var last = persons[persons.length - 1];
    // Domingo
    
    var lastTwo = _.last(persons);
    // Domingo

Conclusion

Lodash is, without a doubt, an extremely useful library. I would continue to use it in all of my NodeJS project.

The advantages that stand out the most is the reduced number of repetitive codes and the improved quality of application logic.

However, what has helped me the most has been the ability to think in a more functional way. I shred my application into smaller modules with a singular focus and no side effects.

This modularity also allows me to write a higher quality code.