Open In App
Related Articles

Underscore.js _.difference() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Underscore.js _.difference() function returns the values of the array that are not present in the other arrays. If the element is present in the array then this element will not be included in the resultant array. This is because it takes a difference between the second array and the first array.

Syntax:

_.difference( array, *others );

Parameters:

  • array: This parameter is used to hold the array elements.
  • others: It is an array whose elements need to be deleted.

Return value:

This function returns an array that contains elements of the first array that are not in the second array.

Passing a list of numbers to _.difference() function:

The ._difference() function takes the element from the list one by one and checks if that element is present in the second array or not. If it is present then it simply ignores the element otherwise add the elements in the resultant array.

Example: This example shows passing a list of numbers to _.difference() function.

html

<!DOCTYPE html>
<html>
    <head>
        <script src =
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.difference([1, 2, 3, 4, 5],
                                    [1, 2, 3, 4, 6, 7]));
        </script>
    </body>
</html>                    

                    

Output:

Passing false values to the _.difference() function:

Passing the false values like null, undefined, false, “” (empty string) then the _.difference() function will work in the same way. If any of the false values are present in the first array but not present in the second array then it will be included in the resultant array.

Example: This example shows passing false values to _.difference() function.

html

<!DOCTYPE html>
<html>
    <head>
        <script src =
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.difference([undefined, '', " ", null],
                                    [undefined, 4, null]));
        </script>
    </body>
</html>                    

                    

Output:

Passing a set of strings to the _.difference() function:

Passing the set of strings which contains words inside “” then the _.difference() function will work in the same way. If any of the these string values are present in the first array but not present in the second array then it will be included in the resultant array.

Example: This example shows passing a set of strings to _.difference() function.

html

<!DOCTYPE html>
<html>
    <head>
        <script src =
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.difference(
                ["This", "is", "geeks", "for", "geeks2"],
                ["This", "for", "is"])
            );
        </script>
    </body>
</html>                    

                    

Output:

Passing two arrays which have the same elements to the _.difference() function:

Passing the first and the second array having the same elements then all the elements during the check operation will be ignored. And hence the resultant array will be empty.

Example: This example shows passing two arrays which have the same elements to _.difference() function.

html

<!DOCTYPE html>
<html>
    <head>
        <script src =
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.difference([1, 2, 3, 4], [1, 2, 3, 4]));
        </script>
    </body>
</html>                    

                    

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.

<script type="text/javascript" src = 
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>


Last Updated : 14 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads