1// Use to remove duplicate elements from the array
2
3const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]
4
5//spreading numbers of the object into an array using the new operator
6console.log([...new Set(numbers)])
7
8// [2, 3, 4, 5, 6, 7, 32]
1let mySet = new Set()
2
3mySet.add(1) // Set [ 1 ]
4mySet.add(5) // Set [ 1, 5 ]
5mySet.add(5) // Set [ 1, 5 ]
6mySet.add('some text') // Set [ 1, 5, 'some text' ]
7let o = {a: 1, b: 2}
8mySet.add(o)
9
10mySet.add({a: 1, b: 2}) // o is referencing a different object, so this is okay
11
12mySet.has(1) // true
13mySet.has(3) // false, since 3 has not been added to the set
14mySet.has(5) // true
15mySet.has(Math.sqrt(25)) // true
16mySet.has('Some Text'.toLowerCase()) // true
17mySet.has(o) // true
18
19mySet.size // 5
20
21mySet.delete(5) // removes 5 from the set
22mySet.has(5) // false, 5 has been removed
23
24mySet.size // 4, since we just removed one value
25
26console.log(mySet)
27// logs Set(4) [ 1, "some text", {…}, {…} ] in Firefox
28// logs Set(4) { 1, "some text", {…}, {…} } in Chrome
29
1// set is used for storing unique values
2const firstSet = new Set([1, 2, 3]);
3
4firstSet.add('hi'); //adding value to set
5
6firstSet.add(3); //this will not give any error and it will also not be added
7
8firstSet.delete('hi');//removing value from set
9
10console.log(firstSet.has('hi'));//checking 'hi' is in the set or not
11
12// showing all values in the set
13console.log(firstSet);
14for (const entry of firstSet.values()) {
15 console.log(entry);
1A set is a collection of items which are unique i.e no element can be repeated.
2Set in ES6 are ordered: elements of the set can be iterated in the insertion
3order. Set can store any types of values whether primitive or objects.
4var set4 = new Set();
1let text = 'India'
2
3let mySet = new Set(text) // Set ['I', 'n', 'd', 'i', 'a']
4mySet.size // 5
5
6//case sensitive & duplicate ommision
7new Set("Firefox") // Set(7) [ "F", "i", "r", "e", "f", "o", "x" ]
8new Set("firefox") // Set(6) [ "f", "i", "r", "e", "o", "x" ]
9
1// iterate over items in set
2// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
3for (let item of mySet) console.log(item)
4
5// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
6for (let item of mySet.keys()) console.log(item)
7
8// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
9for (let item of mySet.values()) console.log(item)
10
11// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
12// (key and value are the same here)
13for (let [key, value] of mySet.entries()) console.log(key)
14
15// convert Set object to an Array object, with Array.from
16let myArr = Array.from(mySet) // [1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}]
17
18// the following will also work if run in an HTML document
19mySet.add(document.body)
20mySet.has(document.querySelector('body')) // true
21
22// converting between Set and Array
23mySet2 = new Set([1, 2, 3, 4])
24mySet2.size // 4
25[...mySet2] // [1, 2, 3, 4]
26
27// intersect can be simulated via
28let intersection = new Set([...set1].filter(x => set2.has(x)))
29
30// difference can be simulated via
31let difference = new Set([...set1].filter(x => !set2.has(x)))
32
33// Iterate set entries with forEach()
34mySet.forEach(function(value) {
35 console.log(value)
36})
37
38// 1
39// 2
40// 3
41// 4