Set
Overview
The Set object lets you store unique values of any type, whether primitive values or object references. For more information see: Set (MDN).
Properties Summarized
Methods Summarized
The set() method adds or updates an entry in a Set object with a specified key and a value.
The entries() method returns a new iterator object that contains the [key, value] pairs for each element in the Set object in insertion order.
void
The forEach() method executes a provided function once for each value in the Set object, in insertion order.
The has() method returns a boolean indicating whether an element with the specified value exists in a Set object or not.
The keys() method returns a new iterator object that contains the keys for each element in the Set object in insertion order.
Properties Detailed
size
The size accessor property returns the number of elements in a Set object.
Type Number
Sample
set.size;
Methods Detailed
add(value)
The set() method adds or updates an entry in a Set object with a specified key and a value.
Parameters
Object value ;
Returns: Set the Set itself
Sample
set.add(value);
clear()
The clear() method removes all elements from a Set object.
Returns: void
Sample
set.clear();
delete(value)
The delete() method removes the specified element from a Set object by key.
Parameters
Object value ;
Returns: Boolean True if the specified element was successfully removed from the Set; false otherwise.
Sample
var success = set.delete(key);
entries()
The entries() method returns a new iterator object that contains the [key, value] pairs for each element in the Set object in insertion order. In this particular case, this iterator object is also an iterable, so the for-of loop can be used..
Returns: Iterator the iterator that can be used in for of loops
Sample
for(var entry of set.entries()) {}
forEach(callback, thisArgument)
The forEach() method executes a provided function once for each value in the Set object, in insertion order.
Parameters
Returns: void
Sample
set.forEach(function(keyValuePair) {});
has(key)
The has() method returns a boolean indicating whether an element with the specified value exists in a Set object or not.
Parameters
Object key ;
Returns: Boolean True if the Set contains an element with the specified key; false otherwise.
Sample
var containsKey = set.has(key);
keys()
The keys() method returns a new iterator object that contains the keys for each element in the Set object in insertion order. In this particular case, this iterator object is also an iterable, so a for...of loop can be used.
Returns: Iterator the iterator that can be used in for of loops
Sample
var values = set.keys();
values()
The values() method returns a new iterator object that contains the values for each element in the Set object in insertion order.
Returns: Iterator the iterator that can be used in for of loops
Sample
var values = set.values();
Last updated
Was this helpful?