Set in JavaScript

Values in a new Set() are unique, they only occur once. You can mySet.add(), mySet.delete(), mySet.clear(). You can check its mySet.size or if it mySet.has("a").

const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]
[...new Set(numbers)] // [2, 3, 4, 5, 6, 7, 32]

Avoid that pitfall: If you add objects like in the following example, remember that you create a different object each time:

const mySet = new Set();
mySet.add({a: "1"});
mySet.add({a: "1"}); // not the same as first object

for (val of mySet) {
  console.log(val);
}

mySet.delete({a: "1"}); // makes no sense, because it is a new object

mySet.size; // 2

Set values are unique and case sensitive:

et text = 'India'

let mySet = new Set(text)  // Set ['I', 'n', 'd', 'i', 'a']
mySet.size  // 5

//case sensitive & duplicate omission
new Set("Firefox")  // Set(7) [ "F", "i", "r", "e", "f", "o", "x" ]
new Set("firefox")  // Set(6) [ "f", "i", "r", "e", "o", "x" ]

Convert Array to Set (and vice versa)

You can construct Sets with an Iterable:

const mySet = new Set([1, 2, 3, 4, 5]);
mySet.has(1); // true
mySet.has(5); // true
mySet.has(6); // false

From Set to Array

[...mySet]

Get entries, keys and values as Iterator

You can get entries(), keys() and values():

const set1 = new Set(["a", "b", "c"]);
const it = set1.entries();
it.next(); // {done: false, value: ["a", "a"] }
it.next(); // {done: false, value: ["b", "b"] }
it.next(); // {done: false, value: ["c", "c"] }
it.next(); // {done: true, value: undefined }
const it = set1.keys()
it.next(); // {done: false, value: "a" }
const it = set1.values()
it.next(); // {done: false, value: "a" }

Helpful Set operations

These I copied straight from MDN:

function isSuperset(set, subset) {
    for (let elem of subset) {
        if (!set.has(elem)) {
            return false
        }
    }
    return true
}

function union(setA, setB) {
    let _union = new Set(setA)
    for (let elem of setB) {
        _union.add(elem)
    }
    return _union
}

function intersection(setA, setB) {
    let _intersection = new Set()
    for (let elem of setB) {
        if (setA.has(elem)) {
            _intersection.add(elem)
        }
    }
    return _intersection
}

function symmetricDifference(setA, setB) {
    let _difference = new Set(setA)
    for (let elem of setB) {
        if (_difference.has(elem)) {
            _difference.delete(elem)
        } else {
            _difference.add(elem)
        }
    }
    return _difference
}

function difference(setA, setB) {
    let _difference = new Set(setA)
    for (let elem of setB) {
        _difference.delete(elem)
    }
    return _difference
}

// Examples
let setA = new Set([1, 2, 3, 4])
let setB = new Set([2, 3])
let setC = new Set([3, 4, 5, 6])

isSuperset(setA, setB)          // => true
union(setA, setC)               // => Set [1, 2, 3, 4, 5, 6]
intersection(setA, setC)        // => Set [3, 4]
symmetricDifference(setA, setC) // => Set [1, 2, 5, 6]
difference(setA, setC)          // => Set [1, 2]

About Author

Mathias Bothe To my job profile

I am Mathias, born 39 years ago in Heidelberg, Germany. Today I am living in Munich and Stockholm. I am a passionate IT freelancer with more than 15 years experience in programming, especially in developing web based applications for companies that range from small startups to the big players out there. I am founder of bosy.com, creator of the security service platform BosyProtect© and initiator of several other software projects.