- Introduction
- Properties
- Methods
- example: remove duplicates from array
- example: common set operations
The Set object lets you store unique values of any type, whether primitive values or object references.
The Set
constructor accepts an iterable like a string or an array; all of its elements will be added to the new Set. If you don't specify this parameter, the new Set is empty.
const set1 = new Set();
const set2 = new Set(['apple', 1, 2, 3]);
const set3 = new Set('apple', 1, 2, 3);
// const set4 = new Set(1, 2, 3, 'apple');
// TypeError: number 1 is not iterable
console.log(set1);
// Set(0) {}
console.log(set2);
// Set(4) { 'apple', 1, 2, 3 }
console.log(set3);
// Set(4) { 'a', 'p', 'l', 'e' }
Sets have a size
property instead of length.
const set1 = new Set(['apple', 1, 2, 3]);
console.log(set1.size);
// 4
The add()
method appends a new element with a specified value to the end of a Set object. Duplicate items will be ignored. If you want to add multiple items you can chain the method.
const set1 = new Set(['apple', 1, 2, 3]);
set1.add(4);
set1.add('apple').add('orange').add('lemon');
console.log(set1);
// Set { 'apple', 1, 2, 3, 4, 'orange', 'lemon' }
The clear()
method removes all elements from a Set object.
const set1 = new Set(['apple', 1, 2, 3]);
set1.clear();
console.log(set1);
// Set {}
console.log(set1.size);
// 0
The delete()
method removes the specified element from a Set object. If the specified value doesn't exist, it's ignored.
const set1 = new Set(['apple', 1, 2, 3]);
set1.delete('apple')
console.log(set1);
// Set { 1, 2, 3 }
The entries()
method returns a new Iterator object that contains an array of [value, value] for each element in the Set object, in insertion order. This seems a bit weird, but it's doing this to keep the API similar to the Map
object. Since sets don't have keys, it uses the value as the key.
const set1 = new Set(['run', 'swim', 'hike']);
const iterator = set1.entries();
console.log(typeof iterator, Array.isArray(iterator));
// object false
for (let i of iterator) {
console.log(i);
console.log(typeof i, Array.isArray(i));
// [ 'run', 'run' ]
// object true
// [ 'swim', 'swim' ]
// object true
// [ 'hike', 'hike' ]
// object true
}
As an iterator, you can also use the next()
method. Note that these iterators are like Python generators in that when you've cycled through all the elements, it's done.
const set1 = new Set(['run', 'swim', 'hike']);
const iterator = set1.entries();
console.log(iterator.next().value);
// [ 'run', 'run' ]
console.log(iterator.next().value);
// [ 'swim', 'swim' ]
console.log(iterator.next().value);
// [ 'hike', 'hike' ]
console.log(iterator.next().value);
// undefined
The forEach() method executes a provided function (callback) once for each value in the Set object, in insertion order.
The callback is invoked with three arguments:
- the element value
- the element key
- the Set object being traversed
There are no keys in Set objects, so the first two arguments are both values contained in the Set. Like with the entries()
method, this is to make it consistent with other forEach() methods for Map and Array.
const set1 = new Set(['run', 'swim', 'hike']);
function imgFromSet(key, value, set) {
let img = `<img src="${value}.png" alt="${key}" />`;
console.log(img);
}
set1.forEach(imgFromSet);
// <img src="run.png" alt="run" />
// <img src="swim.png" alt="swim" />
// <img src="hike.png" alt="hike" />
That being said, looks like you can just do this to:
const set1 = new Set(['run', 'swim', 'hike']);
function imgFromSet(value) {
let img = `<img src="${value}.png" alt="${value}" />`;
console.log(img);
}
set1.forEach(imgFromSet);
// <img src="run.png" alt="run" />
// <img src="swim.png" alt="swim" />
// <img src="hike.png" alt="hike" />
The has()
method returns a boolean indicating whether an element with the specified value exists in a Set object.
const set1 = new Set(['run', 'swim', 'hike']);
console.log(set1.has('swim'));
// true
console.log(set1.has('dance'));
// false
The values()
method returns a new Iterator object that contains the values for each element in the Set object in insertion order.
There is also a keys()
method which is an alias for this method (for similarity with Map objects); it behaves exactly the same and returns values of Set elements.
const set1 = new Set(['run', 'swim', 'hike']);
const iterator = set1.values();
console.log(iterator.next().value);
// 'run'
console.log(iterator.next().value);
// 'swim'
console.log(iterator.next().value);
// 'hike'
console.log(iterator.next().value);
// undefined
// To remove duplicate elements from an array:
const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]
console.log([...new Set(numbers)])
// [2, 3, 4, 5, 6, 7, 32]
function isSuperset(set, subset) {
for (const elem of subset) {
if (!set.has(elem)) {
return false;
}
}
return true;
}
function union(setA, setB) {
const _union = new Set(setA);
for (const elem of setB) {
_union.add(elem);
}
return _union;
}
function intersection(setA, setB) {
const _intersection = new Set();
for (const elem of setB) {
if (setA.has(elem)) {
_intersection.add(elem);
}
}
return _intersection;
}
function difference(setA, setB) {
const _difference = new Set(setA);
for (const elem of setB) {
_difference.delete(elem);
}
return _difference;
}
function symmetricDifference(setA, setB) {
const _difference = new Set(setA);
for (const elem of setB) {
if (_difference.has(elem)) {
_difference.delete(elem);
} else {
_difference.add(elem);
}
}
return _difference;
}
// Examples
const setA = new Set([1, 2, 3, 4])
const setB = new Set([2, 3])
const setC = new Set([3, 4, 5, 6])
isSuperset(setA, setB) // returns true
union(setA, setC) // returns Set {1, 2, 3, 4, 5, 6}
intersection(setA, setC) // returns Set {3, 4}
difference(setA, setC) // returns Set {1, 2}
symmetricDifference(setA, setC) // returns Set {1, 2, 5, 6}