Open In App

Underscore.js _.union() Function

Last Updated : 16 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Underscore.js _.union() function is used to take n number of arrays and return a new array with the unique terms in all those arrays (union of all array). In the new array, the order of the elements is the same as it is mentioned in all the passed arrays. The first occurrence of each array is only included in the resultant array.

Syntax:

_.union( *arrays );

Parameters:

  • arrays: which is the collection of multiple array lists. The array list is separated by “,” the operator.

Return value:

It returns an array that contains unique elements of all the elements in n passed arrays.

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

The ._union() function takes the element from the list one by one and checks whether it is already present in the resultant array or not. If it is present then it just ignores it otherwise adds it to the resultant array. The final result contains the union of the array.

Example: This example shows the use of the _.union() function by passing a list of numbers.

HTML




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


Output:

Passing a combination of words, false values and numbers to the _.union() function:

Passing any kind of elements whether it is a number, word or even false elements like empty strings, null values etc, the _.union() function will not distinguish between them. It will rather treat every element in the same manner. The further process will be the same.

Example: This example shows the use of the _.union() function by passing a combination of words, false values and numbers.

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.union(["gfg", 52, " ", 1, "hello"],
            ['*', 2, undefined, 4],
            ['', null],
            ["gfg2", "end"]));
    </script>
</body>
 
</html>


Output:

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

Pass a set of strings to this function so as to get the common of all the n arrays passed in the result. The processing will occur in the same way. Only the words given in the second parameter will be excluded.

Example: This example shows the use of the _.union() function by passing a set of strings.

html




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


Output:

Passing arrays with same elements to the _.union() function:

If pass arrays to the _.union() function and they have the same elements then union of all the arrays will be the first array itself. All the elements will be common and hence will be present in the result given after union.

Example: This example shows the use of the _.union() function by passing arrays with same elements.

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.union([100, 200],
            [100, 200],
            [100, 200],
            [100, 200],
            [100, 200],
            [100, 200]));
    </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>


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads