Sets

A set is a collection of unique values (duplicates are ignored).

The API for a set is similar to map. The add(..) method takes the place of the set(..) method (somewhat ironically), and there is no get(..) method.

Consider:

  1. var s = new Set();
  2. var x = { id: 1 },
  3. y = { id: 2 };
  4. s.add( x );
  5. s.add( y );
  6. s.add( x );
  7. s.size; // 2
  8. s.delete( y );
  9. s.size; // 1
  10. s.clear();
  11. s.size; // 0

The Set(..) constructor form is similar to Map(..), in that it can receive an iterable, like another set or simply an array of values. However, unlike how Map(..) expects entries list (array of key/value arrays), Set(..) expects a values list (array of values):

  1. var x = { id: 1 },
  2. y = { id: 2 };
  3. var s = new Set( [x,y] );

A set doesn’t need a get(..) because you don’t retrieve a value from a set, but rather test if it is present or not, using has(..):

  1. var s = new Set();
  2. var x = { id: 1 },
  3. y = { id: 2 };
  4. s.add( x );
  5. s.has( x ); // true
  6. s.has( y ); // false

Note: The comparison algorithm in has(..) is almost identical to Object.is(..) (see Chapter 6), except that -0 and 0 are treated as the same rather than distinct.

Set Iterators

Sets have the same iterator methods as maps. Their behavior is different for sets, but symmetric with the behavior of map iterators. Consider:

  1. var s = new Set();
  2. var x = { id: 1 },
  3. y = { id: 2 };
  4. s.add( x ).add( y );
  5. var keys = [ ...s.keys() ],
  6. vals = [ ...s.values() ],
  7. entries = [ ...s.entries() ];
  8. keys[0] === x;
  9. keys[1] === y;
  10. vals[0] === x;
  11. vals[1] === y;
  12. entries[0][0] === x;
  13. entries[0][1] === x;
  14. entries[1][0] === y;
  15. entries[1][1] === y;

The keys() and values() iterators both yield a list of the unique values in the set. The entries() iterator yields a list of entry arrays, where both items of the array are the unique set value. The default iterator for a set is its values() iterator.

The inherent uniqueness of a set is its most useful trait. For example:

  1. var s = new Set( [1,2,3,4,"1",2,4,"5"] ),
  2. uniques = [ ...s ];
  3. uniques; // [1,2,3,4,"1","5"]

Set uniqueness does not allow coercion, so 1 and "1" are considered distinct values.