Underscore.js | _.uniq() with Examples

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 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], [iteratee] )

Parameters: This function accepts three parameters which are listed below:

  • array: This parameter is used to hold the array of elements.
  • isSorted: It is optional parameter. This parameter is used to hold true for sorted array.
  • iteratee: It is optional parameter which is used ho hold iteratee 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


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 the similar manner as in the first example. All the unique elements will be present in the resultant array.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


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 the 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

Passing words to the _.uniq() function: If pass the set of strings to the _.uniq() function then it will work in the 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

Note: These commands will not work in Google console or in Firefox as for 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type="text/javascript" src
</script

chevron_right




My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.