

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the importance of _.union() method in JavaScript?
_.union()
_.Union() method belongs to underscore.js a library of javascript. 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). It scrutinizes each and every value of the arrays and pushes out unique values into another array.
syntax
_.union( array1, array2, .... );
Example
In the following example, _.union() is used to get an array of unique elements.
<html> <body> <script src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > </script> </head> <body> <script type="text/javascript"> document.write(_.union([1, 2, 9, 40], [1, 20, 3, 2], [9, 2])); </script> </body> </html>
Output
1,2,9,40,20,3
The arrays can consist of not only numbers or strings but also empty strings or falsy values.
Example
In the following example, all kinds of values are used in arrays. Even though, _.union() has done its task of giving a new array with unique values.
<html> <body> <script src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > </script> </head> <body> <script type="text/javascript"> document.write(_.union(["hello",2, "hi", 1, ""], ['the', undefined], ['', null], ["*", ""])); </script> </body> </html>
Output
hello,2,hi,1,,the,,,*
- Related Questions & Answers
- What is the importance of Math.trunc() method in JavaScript?
- What is the importance of _isEqual() method in JavaScript?
- What is the importance of _without() method in JavaScript?
- What is the importance of str.padStart() method in JavaScript?
- What is importance of startsWith() method in JavaScript?
- What is the importance of Symbol.isConcatSpreadable in JavaScript?
- What is the importance of _.initial() function in JavaScript?
- What is the importance of Navigator Object in JavaScript?
- What is the importance of hygiene?
- What is the importance of Computer?
- Write the importance of shift() method in javascript array?
- What is the importance of '/g' flag in JavaScript?
- What is the importance of '/i' flag in JavaScript?
- What is the importance of Hindu rituals?
- What is the importance of operating systems?
Advertisements