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

Type
Name
Summary

The size accessor property returns the number of elements in a Set object.

Methods Summarized

Type
Name
Summary

The set() method adds or updates an entry in a Set object with a specified key and a value.

void

The clear() method removes all elements from a Set object.

The delete() method removes the specified element from a Set object by key.

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.

The values() method returns a new iterator object that contains the values 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

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

Returns: Boolean

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

Returns: Boolean

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