Šajā piemērā jūs iemācīsities rakstīt JavaScript programmu, kas ilustrēs dažādas kopu darbības.
Lai saprastu šo piemēru, jums jāpārzina šādas JavaScript programmēšanas tēmas:
- JavaScript kopa un WeakSet
- JavaScript lokam…
- JavaScript funkciju un funkciju izteiksmes
1. piemērs: iestatiet Savienības darbību
// perform union operation // contain elements of both sets function union(a, b) ( let unionSet = new Set(a); for (let i of b) ( unionSet.add(i); ) return unionSet ) // two sets of fruits const setA = new Set(('apple', 'mango', 'orange')); const setB = new Set(('grapes', 'apple', 'banana')); const result = union(setA, setB); console.log(result);
Rezultāts
Komplekts ("ābols", "mango", "apelsīns", "vīnogas", "banāns")
Komplekta savienojuma darbība apvieno abu kopu elementus vienā.
Jauns komplekts unionSet
tiek izveidots, izmantojot new Set()
. Mainīgais unionSet satur visas setA vērtības. Tad for… of
cilpa tiek izmantota, lai atkārtotu visus setB elementus un pievienotu tos unionSet, izmantojot add()
metodi.
Komplektā nav vērtību kopiju. Tādējādi, ja kopa satur to pašu vērtību, pēdējā vērtība tiek izmesta.
2. piemērs: iestatiet krustošanās darbību
// perform intersection operation // elements of set a that are also in set b function intersection(setA, setB) ( let intersectionSet = new Set(); for (let i of setB) ( if (setA.has(i)) ( intersectionSet.add(i); ) ) return intersectionSet; ) // two sets of fruits const setA = new Set(('apple', 'mango', 'orange')); const setB = new Set(('grapes', 'apple', 'banana')); const result = intersection(setA, setB); console.log(result);
Rezultāts
Komplekts ("ābols")
Iestatītā krustošanās darbība apzīmē elementus, kas atrodas gan setA, gan setB.
Jauns komplekts intersectionSet
tiek izveidots, izmantojot new Set()
. Tad for… of
cilpa tiek izmantota iterācijai caur setB. Katram elementam, kas atrodas gan setA, gan setB, tie tiek pievienoti krustojuma kopai.
3. piemērs: iestatiet atšķirības darbību
// perform difference operation // elements of set a that are not in set b function difference(setA, setB) ( let differenceSet = new Set(setA) for (let i of setB) ( differenceSet.delete(i) ) return differenceSet ) // two sets of fruits const setA = new Set(('apple', 'mango', 'orange')); const setB = new Set(('grapes', 'apple', 'banana')); const result = difference(setA, setB); console.log(result);
Rezultāts
Komplekts ("mango", "oranžs")
Kopas starpības darbība apzīmē elementus, kas atrodas vienā kopā, nevis citā kopā.
StarpībaSet satur visus setA elementus. Tad for… of
cilpa tiek izmantota, lai atkārtotu visus setB elementus. Ja elements B, kas atrodas komplektā B, ir pieejams arī komplektā A, šis elements tiek izdzēsts, izmantojot delete()
metodi.
4. piemērs: iestatiet apakškopas darbību
// perform subset operation // true if all elements of set b is in set a function subset(setA, setB) ( for (let i of setB) ( if (!setA.has(i)) ( return false ) ) return true ) // two sets of fruits const setA = new Set(('apple', 'mango', 'orange')); const setB = new Set(('apple', 'orange')); const result = subset(setA, setB); console.log(result);
Rezultāts
taisnība
Komplekta apakškopas darbība atgriež vērtību true, ja visi kopas B elementi ir kopā A.
for… of
Cilpa tiek izmantota, lai cilpas cauri elementiem SETB. Ja kāds elements, kas atrodas, ir iestatīts, B nav komplektā A, false
tiek atgriezts.