The Underscore.js is a JavaScript library that provides a lot of useful functions like the map, filter, invoke, etc even without using any built-in objects. The _.uniq() function returns the array which does not contain duplicate elements. The first occurrence of the element is included in the resultant array. The operation of checking whether the array is duplicate or not. It is done by the ‘===’ operation.
Syntax:
_.uniq( array, [isSorted], [iterate] )
Parameters: This function accepts three parameters which are listed below:
- array: This parameter is used to hold the array of elements.
- isSorted: It is an optional parameter. This parameter is used to hold true for sorted arrays.
- iterate: It is an optional parameter that is used to hold the iterate function.
Return value: It returns an array of unique elements.
Passing a list of numbers to _.uniq() function: The ._uniq() function takes the element from the list one by one and checks whether it is in the resultant array (which is initially empty) by the ‘===’ operator. If it is present then it ignores it and checks the next element. Otherwise, since it is the first occurrence of the element so it is included in the resultant array.
Example:
html
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
</ head >
< body >
< script type="text/javascript">
console.log(_.uniq([1, 2, 3, 4, 5, 4, 3, 2, 1]));
</ script >
</ body >
</ html >
|
Output:

Passing the second parameter as false to the _.uniq() function: If pass the second parameter as false along with the array then the _.uniq() function will work in a similar manner as in the first example. All the unique elements will be present in the resultant array.
Example:
html
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
</ head >
< body >
< script type="text/javascript">
console.log(_.uniq([10, 0, 5, 1, 6, 10, 2, 1, 2], false));
</ script >
</ body >
</ html >
|
Output:

Passing the second parameter as true to the _.uniq() function: If pass the second parameter as true along with the array then the _.uniq() function will not work in a similar manner rather it will perform any operation on the array. Hence, the resultant array will contain all the elements of the array passed in the same order in which it appeared in the passed array.
Example:
html
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
</ head >
< body >
< script type="text/javascript">
console.log(_.uniq([10, 0, 5, 1, 6, 10, 2, 1, 2], true));
</ script >
</ body >
</ html >
|
Output:

Passing words to the _.uniq() function: If passing the set of strings to the _.uniq() function then it will work in a similar manner as it will work with numbers etc. Therefore, the resultant array will contain only the first occurrence of all the repeated elements in the resultant array.
Example:
html
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
</ head >
< body >
< script type="text/javascript">
console.log(_.uniq(["HTML", "CSS", "JS",
"AJAX", "CSS", "JS", "CSS"]));
</ script >
</ body >
</ html >
|
Output:

Note: These commands will not work in Google Console or in Firefox as these additional files need to be added which they didn’t have added. So, add the given links to your HTML file and then run them.
html
< script type="text/javascript" src =
</ script >
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 May, 2023
Like Article
Save Article