The _.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 same like it is mentioned in all the passed arrays. The first occurrence of each array is only included in the resultant array.
_.union( *arrays )
Parameters: This function accepts single parameter arrays which is the collection of multiple array list. The array list are separated by , operator.
Return value: It returns an array which 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 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 array.
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.
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.
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.
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.
- PHP | Ds\Map union() Function
- TypeScript Union
- PHP | Ds\Set union() Function
- Underscore.js | every() with Examples
- jQuery | one() with Examples
- jQuery | last() with Examples
- Underscore.js | map() with Examples
- jQuery | on() with Examples
- jQuery | after() with Examples
- jQuery | first() with Examples
- jQuery | eq() with Examples
- Underscore.js | contains() with Examples
- Underscore.js | _.last() with Examples
- jQuery | has() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.