Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our day-to-day common programming tasks.
Array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable. Underscore.js provides a wide range of array functions that are used to manipulate the given array values.
Function
|
Description
|
_.first() |
It returns the first element of the array, i.e. the number at the zeroth index. |
_.initial() |
It excludes the last element from the array. |
_.last() |
It is used to display the last element of the array. |
_.rest() |
It returns the rest of the elements except the zeroth indexed element. |
_.compact() |
It returns an array after removing all the false values. |
_.flatten() |
It is used to flatten an array which is nested to some level. |
_.without() |
It returns a copy of array which contains all the array except values. |
_.union() |
It takes n number of arrays and return a new array with the unique terms in all those arrays. |
_.intersection() |
It is used to find the intersection of passed arrays. |
_.difference() |
It returns the values of array that are not present in the other arrays. |
_.uniq() |
It returns the array which does not contain duplicate elements. |
_.zip() |
It matches each passed array of elements to the next passed array element. |
_.unzip() |
It combines the same type of elements of different arrays into a single array. |
_.object() |
It It converts the array elements into objects. |
_.chunk() |
It converts a single array into a number of arrays. |
_.indexOf() |
It gives the index of the elements whose position we need to find. |
_.lastIndexOf() |
It finds the index of an element in an array. |
_.sortedIndex() |
It decides where the new element you want to insert into the passed array. |
_.findIndex() |
It finds the index of an element which is passed in the second parameter. |
_.findLastIndex() |
It finds the position of the element in an array starting from the end of the array. |
_.range() |
It prints the list of elements from the start given as a parameter to the end also a parameter. |