Some advantages of using Lo-Dash

Lodash is a JavaScript utility library just like jQuery…

lodash

Lodash is a JavaScript utility library just like jQuery and its newest version (v3.6.0)was released just 2 days ago and it is the most depended-upon package in NPM.

Its author is John David Dalton and started it from a Underscore.js fork with the purpose to provide a more consistent cross browser behaviour with improved performance. In order to access its tools just lie jQuery uses ‘$’ to access them, in this case Lodash uses ‘_’.

Now, to install the library we can get the library from the following url:

http://raw.github.com/lodash/lodash/3.6.0/lodash.js

Then we can install it in our browser using

script src=“yourpath/lodash.js

OR using nom package manager

npm i -g npm
npm i —save lodash

SAMPLES

Code is cleaner and is easy to accomplish things with fewer lines of code. For example to make a sum of 2 numbers it is done as


below. _.add(81,2);

To get a max value from an array of integers we can use

_.max([5,3,100,2,1]);

Listing objects from a JSON object.
Suppose we have a nested JSON file. We can iterate through the file using _chain. Assume we have a json with name,surname,fullname, email fields. We can access and list all using

_(json.users).chain()
.value();

If we want to access and get an specific field we can do the following

_(json.users).chain()
.first()
.pick(‘email’)
.value();

This will take the first user from the array and display its email.