Underscore.js | _.intersection() 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 _.intersection() function is used to find the intersection of passed arrays, i.e. the elements which are common in all the n passed arrays in the _.intersection() function. This function find the elements which are present in all the arrays and then apply some operation on those elements then this function is used. It performs the intersection operation at the very basic level.

Syntax:

_.intersection( *arrays )

Parameters: This function accepts single parameter arrays which contains set of array from which the common element need to be find.

Return value: It returns an array which contains the common elements of all the arrays.

Passing a list of numbers to _.intersection() function: The .intersection() function takes the element from the list one by one and then checks whether it is present in the list or not. If it is present in all the other arrays then only it will be included in the resultant array otherwise it is ignored.

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(_.intersection([1, 2, 3, 4, 5], 
                                       [1, 2, 3, 4, 6, 7], 
                                       [1, 2, 6, 8, 9])
            );
        </script>
    </body>
</html>                    

chevron_right


Output:

Passing the false values to the _.intesection() function: If pass the false elements like the null, undefined along with the true elements like strings, numbers etc then the _.intersection() function will work in the same manner. The elements which are common inspite of being a false element will be 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(_.intersection(["gfg", 52, " ", null], 
                                       [undefined, 4, null], 
                                       ["", null],
                                       ["gfg2", null])
            );
        </script>
    </body>
</html>                    

chevron_right


Output:

Passing the words to the _.intesection() function: If pass the words like strings then the _.intersection() function will work in the same manner. The elements which are common inspite of being a string, empty string element will be in the resultant array. Like in the below example only the string “This” matches in all the arrays so it will be displayed.

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(_.intersection(["This", "geeks"], 
                              ['for', "geeks2", "is", "amazing", "This"], 
                              ["This", "is", "best", "platform"])
            );
        </script>
    </body>
</html>                    

chevron_right


Output:

Passing the same array element to the _.intersection() function: Passing the arrays that have same elements then all the elements will be included in the resultant array. This is because all the elements are common to all the passed arrays.

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(_.intersection([1, 2, 3, 4, ""], 
                                       [1, 2, 3, 4, ""], 
                                       [1, 2, 3, 4, ""])
            );
        </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.