Preface
Previous articleJavaScript data structure collection creation (1)We introduced what a collection is and manually implemented a collection class. To summarize, a collection is a set of unique elements and no order of data sets. The key is unique elements.
ES6 provides native collection support, which is newly addedSetData type. In fact, we have almost achieved it in the last article.SetIf you don’t know the collection yet, please read the previous article
But we also said,SetThe basic functions do not include mathematical operations such asIntersection, union, difference, in fact, this is also part of the collection. In this article, we will introduce the operations of this type of set.
1. Set operation
One of the main applications of collections in the computer world isdatabase. For example, in a relational database, the queries we commonly use are basically filtering, merging, filtering and other operations on one or more data sets.
For example, if you write a SQL statement, it may be to obtain all the data in the table, or it may be to obtain part of the data based on the conditions, or it may be an association query, which requires the data of multiple tables at once.
Decide how the collection is handled based on different needs, which is called joining in SQL. The basis of SQL joins is set operations.
Our meta-calculation of the set includes the following:
-
Collect
: Given two sets, return a new set containing all elements in the two sets -
Intersection
: Given two sets, return a new set containing common elements -
Difference set
: Given two sets, return a new set of elements that have the first set and the second set does not have -
Subset
: Verify that a collection is a subset of another collection (part)
Let's see how the corresponding implementation is implemented.
1. Convergence
To put it bluntly, the union is a collection that contains all elements of the two sets but does not repeat them.
In fact, it is easy to understand. We implement a set class based onunion
method.
union(otherSet) { let unionSet = new Set() ().forEach(value=> (value)) ().forEach(value=> (value)) return unionSet; }
As in the above implementation, first instantiate a new set, and then add all elements of the two sets to the new set respectively. Because the collection will perform repeated verification when adding elements, the new collection contains all elements after all, and is not repeated.
2. Intersection
Intersection is a new set of elements shared by two sets, and this set is definitely a subset of the two sets.
Let's do itintersection
method:
intersection(otherSet) { let inters = new Set() let values = () for(let i = 0; i < ; i++) { if((values[i])) { (values[i]) } } return inters; }
This implementation is the same as union, first defining a new set. It is just in the traversal of a collection element, and determine whether the element is in another collection, and if so, it is added to the new collection, so that the new collection is an intersection.
Improve intersection
The function has been implemented, let’s look at another situation. Suppose two sets are as follows:
- Set A: [1, 2, 3, 4, 5, 6, 7]
- Set B: [4, 7]
If we follow the above method, we need to loop seven times to get the intersection. So is there a way to choose a collection loop with a smaller length and implement the function?
Yes, suppose you traverse set B, you only need to loop twice.Let's see how to improve:
intersection(otherSet) { let inters = new Set(); let bigvals = () let lessvals = (); if( < ) { bigvals = (); lessvals = () } for(let i = 0; i < ; i++) { if((lessvals[i])) { (lessvals[i]) } } return inters; }
This method is to first determine which set has a shorter length, then iterate over the shorter set, and then determine whether the element is in another set, thus avoiding unnecessary loops.
3. Differences
The difference set refers to the element that exists in set A but does not exist in set B, that is, calculationA - B
part.
Let's implement the Set classdifferent
method:
different(otherSet) { let diffSet = new Set(); ().forEach(value=> { if(!(value)) { (value) } }) return diffSet; }
It can be seen from the code that the implementation logic of difference sets and intersections is exactly the opposite.
4. Subset
In mathematical concept, if set A is included in set B, that is, all elements in set A exist in set B, then we think set A is a subset of set B.
From a program perspective, set A is a part filtered out from set B, so set A is a subset.
Let's implement subsetisSubsetOf
method:
isSubsetOf(otherSet) { let isSubset = true let values = () for(let i = 0; i < ; i++) { if(!(values[i])) { isSubset = false; break; } } return isSubset; }
This method is to detect whether each element exists in the instance set in the parameter set. If there is one that does not exist, it means that the parameter set is not a subset, terminates the loop and returns the result.
There are actually easier ways:
isSubsetOf(otherSet) { return ().every(value=> (value)) }
every
The method can determine whether each element meets the criteria. If it matches, returntrue
, otherwise returnfalse
。
2. Use set operations
The above has completed the implementation of basic set operations, let’s use it now:
let setA = new Set() ('Beijing') ('Shanghai') ('Guangzhou') let setB = new Set() ('Beijing') ('Nanjing') ('Wuhan')
First add two sets, and then use them to test the basic meta-abiding:
let sets = (setB); (()); // ['Beijing', 'Shanghai', 'Guangzhou', 'Nanjing', 'Wuhan'] let inters = (setB); (()); // ['Beijing'] let diffs = (setB); (()); // ['Shanghai', 'Guangzhou']
Finally, test the subset:
let issub = (setB); (issub); // false let setC = new Set(); ("Shanghai"); issub = (setC); (issub); // true
Passed the test and achieved perfectly!
3. Summary
Through two articles, you have introduced the relevant knowledge of the collection. Have you learned it? Although ES6 provides native support, for us learners, implementing it manually once is more helpful to understand the principles.
This is the end of this article about collection creation (2) of JavaScript data structures. For more related JavaScript collection content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!