Class Set

A Set can store any kind of element and ensures that no duplicates exist. Objects are stored and iterated in insertion order.

API Version:

Available from version 21.2.

Note

Property Summary 

PropertyDescription
size: NumberNumber of elements stored in this set.

Constructor Summary 

ConstructorDescription
Set()Creates an empty Set.
Set(Iterable)If the passed value is null or undefined then an empty set is constructed.

Method Summary 

MethodDescription
add(Object)Adds an element to the set.
clear()Removes all elements from this set.
delete(Object)Removes the element from the set.
entries()Returns an iterator containing all elements of this set.
forEach(Function)Runs the provided callback function once for each element present in this set.
forEach(Function, Object)Runs the provided callback function once for each element present in this set.
has(Object)Returns if this set contains the given object.

Methods inherited from class Object 

assign, create, create, defineProperties, defineProperty, entries, freeze, fromEntries, getOwnPropertyDescriptor, getOwnPropertyNames, getOwnPropertySymbols, getPrototypeOf, hasOwnProperty, is, isExtensible, isFrozen, isPrototypeOf, isSealed, keys, preventExtensions, propertyIsEnumerable, seal, setPrototypeOf, toLocaleString, toString, valueOf, values

Property Details 

size 

size: Number

Number of elements stored in this set.


Constructor Details 

Set() 

Set()

Creates an empty Set.


Set(Iterable) 

Set(values: Iterable)

If the passed value is null or undefined then an empty set is constructed. Else an iterable object is expected that delivers the initial set entries.

Parameters:

  • values - The initial set entries.

Method Details 

add(Object) 

add(object: Object): Set

Adds an element to the set. Does nothing if the set already contains the element.

Parameters:

  • object - The object to add.

Returns:

  • This set object.

clear() 

clear(): void

Removes all elements from this set.


delete(Object) 

delete(object: Object): Boolean

Removes the element from the set.

Parameters:

  • object - The object to be removed.

Returns:

  • true if the set contained the object that was removed. Else false is returned.

entries() 

entries(): ES6Iterator

Returns an iterator containing all elements of this set.


forEach(Function) 

forEach(callback: Function): void

Runs the provided callback function once for each element present in this set.

Parameters:

  • callback - The function to call, which is invoked with three arguments: the element (as value), the element (as index), and the Set object being iterated.

forEach(Function, Object) 

forEach(callback: Function, thisObject: Object): void

Runs the provided callback function once for each element present in this set.

Parameters:

  • callback - The function to call, which is invoked with three arguments: the element (as value), the element (as index), and the Set object being iterated.
  • thisObject - The Object to use as 'this' when executing callback.

has(Object) 

has(object: Object): Boolean

Returns if this set contains the given object.

Parameters:

  • object - The object to look for.

Returns:

  • true if the set contains the object else false is returned.