Set (and WeakSet)

A Set object stores unique values of any type (including primitives). A WeakSet object stores unique objects.

References to objects in a WeakSet collection are held weakly. If there is no other reference to an object stored in the WeakSet, it can be garbage-collected. This means memory leak can be avoided. That also means that there is no list of current objects stored in the collection. A WeakSet is not enumerable, making it a good choice for keeping private data, as an outsider won't be able to list and inspect its contents.A Set has the following properties and methods:

Set.prototype.size

Returns the number of values in the Set object.

Set.prototype.clear()

Removes all elements from the Set object.

Set.prototype.forEach(callbackFn[, thisArg])

Calls callbackFn once for each value present in the Set object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the 'this' value for each callback.

This shows how to merge Sets/Maps.
RESETRUNFULL
<!DOCTYPE html><html><body><script>

var s1 = new Set([...(new Set([1,2,3])),
                           ...(new Set([4,5,6]))]);console.log(s1.size); // 6

</script></body><html>

The following three methods are also shared by WeakSet:

Set.prototype.has(value)

Returns a boolean asserting whether an element is present with the given value in the Set object or not.

Set.prototype.add(value)

Appends a new element with the given value to the Set object. Returns the Set object.

Set.prototype.delete(value)

Removes the element associated with the value and returns the value that Set.prototype.has(value) would have previously returned. Set.prototype.has(value) will return false afterwards.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

var o = {};var s = new Set("hi"); // accepts any iterable valueconsole.log(s.size); // 2;s.add('h'); console.log(s.size); // 2s.add(o);
   console.log(s.size); // 3s.add(o);
   console.log(s.size); // 3s.add({}); console.log(s.size); // 4s.add({}); console.log(s.size); // 5s.add(NaN); console.log(s.size); // 6s.add(NaN); console.log(s.size); // 6s.forEach((v)=>{console.log(v);});  // h,i,Object,Object,Object,NaN

</script></body><html>

RESETRUNFULL
<!DOCTYPE html><html><body><script>

var ws = new WeakSet();var o = {};ws.add([1,2]); console.log(ws.has([1,2]));  // falsews.add(o); ws.add(o); ws.delete(o);console.log(ws.has(o));  // false

</script></body><html>
This shows how to obtain the last value added to a Set.
RESETRUNFULL
<!DOCTYPE html><html><body><script>

var s = new Set([1, 2, 3]);s.add(10);var lastValue = Array.from(s).pop();console.log(lastValue); // 10

</script></body><html>
Testing for equality of Sets is not so straightforward.
RESETRUNFULL
<!DOCTYPE html><html><body><script>

var a = new Set([1,2,3]);var b = new Set([1,3,2]);alert(eqSet(a, b)); // truefunction eqSet(as, bs) {
    if (as.size !== bs.size) return false;
    for (var a of as) if (!bs.has(a)) return false;
    return true;}

</script></body><html>

For information about the following three methods, refer to 8.12.4.

Set.prototype.keys()

Set.prototype.values()

Set.prototype.entries()

A Set allows duplicates to be removed from an array.
RESETRUNFULL
<!DOCTYPE html><html><body><script>

var a = ['a','b','a'];console.log(Array.from((new Set(a)).values())); //["a", "b"]

</script></body><html>
These are some ways to stringify an array-like Set, or an iterable in general.
RESETRUNFULL
<!DOCTYPE html><html><body><script>

JSON.stringify([...s]);JSON.stringify([...s.keys()]);JSON.stringify([...s.values()]);JSON.stringify(Array.from(s));JSON.stringify(Array.from(s.keys()));JSON.stringify(Array.from(s.values()));

</script></body><html>