Normally underscore.js provides different kinds of functions for collection such as each map and reduce. The functions we can apply to the collection, also provide different methods such as groupBy, countBy, min, and max.
Underscore.js also provides the different functions for the array such as initial, lastindexOf, intersection, etc.
When we need to implement the bind, delay, before and after functions, at that time we can also use underscore.js.
We can manipulate the object; we can map the object as well as we can compare the object by using the underscore.js library.
If we need to implement different utilities such as escape, random, and iterate, etc.
Now let’s see how we can set up the underscore.js as follows.
Basically, there are two ways to implement the underscore.js as follows.
1. We can execute the underscore.js file by using the browser:
This is the first way to implement the underscore.js file; in this method, we can directly execute this in the browser. So we need to follow some steps as follows.
1. First we need to go to the official website of understand.js, there is one more option that gives you the latest underscore-min.js file and in this file, we cannot make any changes.
2. After that we need to add the underscore-min.js file into the script and we can start working with the underscore.js file.
Now let’s use the second method as follows.
2. Implement underscore.js by using the Node.js:
This is another method to implement underscore.js but make sure Node.js and npm must be installed in your system. Otherwise, we can use the following command to install the required setup.
npm install underscore
Now let’s see the iterating collection in underscore as follows.
- _.each (): we can write the syntax of this method is _.each (list, iteratee) and it is used to get each parameter. In this method there are two parts such as an array or object to iterate and callback function.
The execution of this method is very easy, in which every object of the array that .each() will callback the function and inside that function again we have different parameters such as the value of array, index or array and iterating of the array that we showed in syntax.
- _.map (): This is the inbuilt function of the underscore.js library. Basically _.map () function is used to create a new array and map each and every element from the list by using a transformation function.
In the above syntax, we use two parameters as follows.
array_list: It is used to store the element into the array list.
Transformation_function: it is a function and it executes all elements from the array list.
Now let’s see how we can pass the element to a user-defined function.
We know that we have an array list that contains the array element and we need to access them one by one by using a user-defined function. In this case, a user-defined function fetches the element one by one and performs the operation that we require, and displays the result on screen by using the console.
Now let’s see some methods of Processing Collections as follows.
- _.contains (): The underscore.js also provides the _.contains () method to check whether the element is present in the list or not.
_.contains (array_list, value_to_search, [array_index])
In the above syntax, we used different parameters as follows.
- array_list: It is used to store the element into the array list.
- value_to_search: This is used to store the element that we need to search.
- array_index: This is an optional part of this syntax; basically it is used to hold the starting index to search.
In processing collections, we have a different method and some are listed below.
Invoke, pluck, max, min, sortBy, groupBy, indexBy, countBy, etc.
Now let’s see iterating array methods as follows.
Underscore.js provides the array iterate method listed below.
first, initial, last, rest, indexOf, lastIndexOf, sortedIndex etc.
Now let’s see the different functions in underscore.js listed below.
By using functions we can easily handle the underscore.js functions as listed below.
Bind, partial, delay, once, wrap, negate, etc.
Underscore.js provides 100+ methods to the user.
In the above example, we write code in an Html file and we use a built-in library that is underscore-min.js. In this program, we try to find the minimum number from the given array list by using _.min () method of underscore.js. The final output of this program we illustrated by using the following screenshot as follows.
So in this way, we can use different methods to perform the different operations.