How to return the union of sets in dojox
What is Dojo?
The Dojo is a JavaScript toolkit (library) that provides many utility modules and functions to create a web application. We can save time and speed up the development process using the predefined Dojo modules. Dojo includes language utilities, UI components, and more.
What is a Set ?
A Set is a collection of elements that will not allow duplicate elements.
The dojox/collections/Set module contains utility functions to perform set operations on JavaScript arrays or dojox.collections.ArrayList objects.
How to get the union of two sets?
We can use the union() method of the dojox/collections/Set class to get the
Syntax
Set.union(setA, setB)
The union() method takes two arguments and returns the union of two sets.
Code example
Let's look at the code below:
Code explanation
Line 6: We load the Dojo library from the CDN server.
Line 8: We load the
dojox/collections/Setmodule. This module contains a utility function to perform set operations.Lines 9–10: After the
Setmodule is loaded, We create two new arraysSetAandSetBwith values[1,2,3]and[3,4,5], respectively.Line 13: We use the
unoin()method of theSetto get the union ofSetAandSetBarray. This method returns[1,2,3,4,5]as result.
Free Resources