Lodash – Quick Guide


Lodash – Quick Guide



”;


Lodash – Overview

Lodash is a popular javascript based library which provides 200+ functions to facilitate web development. It provides helper functions like map, filter, invoke as well as function binding, javascript templating, deep equality checks, creating indexes and so on. Lodash can be used directly inside a browser and also with Node.js.

Working with objects using JavaScript can be quite challenging, specifically if you have lots of manipulation to be done with them. Lodash comes with lots of features that eases your work with objects.

Lodash is an open source project and you can easily contribute to the library and add features in the form of plugins and make it available on GitHub and in Node.js.

Features

Let us understand in detail all the important features available with Lodash −

Collections

Lodash provides various functions for collections like each, map, reduce which are used to apply an operation on each item of a collection. It provides method like groupBy, countBy, max, min which processes collections and ease lot of tasks.

Arrays

Lodash provides various functions for arrays like to iterate and process arrays like first, initial, lastIndexOf, intersection, difference etc.

Functions

Lodash provides functions such as bind, delay, before, after etc.

Objects

Lodash provides functions to manipulate objects, to map objects and comparing objects. For example, keys, values, extends, extendsOwn, isEqual, isEmpty etc.

Utilities

Lodash provides various utilities methods like noConflict, random, iteratee, escape etc.

Chaining

Lodash provides chaining methods as well like chain, value.

In subsequent chapters, we”ll cover important functions of Lodash

Lodash – Environment Setup

In this chapter, you will learn in detail about setting up the working environment of Lodash on your local computer. Before you begin with working on Lodash, you need to have the access to the library. You can access its files in any of the following methods −

Method 1: Using Lodash File in Browser

In this method, we are going to need Lodash file from its official website and will use it directly in the browser.

Step 1

As a first step, go to the official website of Lodash https://lodash.com/.

Observe that there is a download option available which gives you the latest lodash.min.js file CDN Copies available. Click on the link and select latest link for lodash.min.js.

Step 2

Now, include lodash.min.js inside the script tag and start working with Lodash. For this, you can use the code given below −


<script type = "text/JavaScript" 
   src = "https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js">
</script>

Given here is a working example and its output for a better understanding −

Example


<html>
   <head>
      <title>Lodash - Working Example</title>
      <script type = "text/JavaScript" src = "https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
      <style>
         div {
            border: solid 1px #ccc;
            padding:10px;
            font-family: "Segoe UI",Arial,sans-serif;
            width: 50%;
         }
      </style>
   </head>
   <body>
      <div style = "font-size:25px" id = "list"></div>
      <script type = "text/JavaScript">
         var numbers = [1, 2, 3, 4];
         var listOfNumbers = '''';
         _.each(numbers, function(x) { listOfNumbers += x + '' '' });
         document.getElementById("list").innerHTML = listOfNumbers;
      </script>
   </body>
</html>

Output