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:
Then we can install it in our browser using
OR using nom package manager
npm i -g npm npm i —save lodash
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
To get a max value from an array of integers we can use
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
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.