Setオブジェクトは集合を扱うための機能を組み込んだオブジェクトです。
Setオブジェクトの生成には、Setコンストラクターを使います。
const set = new Set()
Setオブジェクトにはリテラルはありません。
[https://paiza.io/projects/xMVHzYIUhKz87MqhdPgqDQ?language=javascript コード例]:
const set1 = new Set() ; console.log(set1)
set1.add(1)            ; console.log(set1)
set1.add("a")          ; console.log(set1)
set1.add(1)            ; console.log(set1)
set1.add([1,2,3])      ; console.log(set1)
set1.delete(1)         ; console.log(set1)
set1.delete(1)         ; console.log(set1)
set1.add(1)            ; console.log(set1)
console.log(`set1.has(1) = ${set1.has(1)}, set1.has(2) = ${set1.has(2)}`)
console.log([...set1])
console.log([...set1.values()])
console.log([...set1.keys()])
console.log([...set1.entries()])
set1.forEach(x => console.log(x))
set1.clear(set1)       ; console.log(set1)
const a = new Set()
const b = new Set()
a.add(100)
b.add(100)
console.log(a == b)
実行結果:
Set(0) {}
Set(1) { 1 }
Set(2) { 1, 'a' }
Set(2) { 1, 'a' }
Set(3) { 1, 'a', [ 1, 2, 3 ] }
Set(2) { 'a', [ 1, 2, 3 ] }
Set(2) { 'a', [ 1, 2, 3 ] }
Set(3) { 'a', [ 1, 2, 3 ], 1 }
set1.has(1) = true, set1.has(2) = false
[ 'a', [ 1, 2, 3 ], 1 ]
[ 'a', [ 1, 2, 3 ], 1 ]
[ 'a', [ 1, 2, 3 ], 1 ]
[ [ 'a', 'a' ], [ [ 1, 2, 3 ], [ 1, 2, 3 ] ], [ 1, 1 ] ]
a
[ 1, 2, 3 ]
1
Set(0) {}
false
存在しない要素をdeleteしても例外は上がりません。
Setオブジェクトは集合ですが、JavaScriptは演算子オーバーロードはできないので集合同士に比較演算子は適用できません。
|url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
|title=Set - JavaScript // MDN
|date=2021/12/08
|accessdate=2021/12/26
。
集合演算関係のメソッド追加提案は既にTC39に提出されていますが{{Cite web
|url=https://tc39.es/proposal-set-methods/
|title=New Set methods
|date=2019
|accessdate=2021/12/26
、参考までに Polyfill を書いてみました。
[https://paiza.io/projects/Zx6wmzOUm7IMcltSD14Cug?language=javascript Polyfill]:
Object.entries({
    filter: function (callback) {
        if (typeof callback !== 'function')
            throw new TypeError();
        
        const result = new Set()
        for (const x of this)
            if (callback(x))
                result.add(x)
        return result
    },
    map: function (callback) {
        if (typeof callback !== 'function')
            throw new TypeError();
        
        const result = new Set()
        for (const x of this)
            result.add(callback(x))
        return result
    },
    reduce: function (callback, initial) {
        if (typeof callback !== 'function')
            throw new TypeError();
        let result = initial
        for (const x of this) {
            if (result === undefined)
                result = x
            else
                result = callback(result, x, this)
        }
        return result
    },
    union: function (other) {
        const result = new Set(this)
        for (const x of other) {
            result.add(x)
        }
        return result
    },
    intersection: function (other) {
        return this.filter(e => other.has(e))
    },
    difference: function (other) {
        return this.filter(e => !other.has(e))
    },
    superset_p: function (other) {
        for (const elem of other) {
            if (!this.has(elem)) {
                return false
            }
        }
        return true
    },
    subset_p: function (other) {
        return other.superset_p(this)
    },
}).forEach(pair => {
    const [method, func] = pair
    Object.defineProperty(Set.prototype, method, {
        value: func
    })
})
const a = new Set([0, 1, 2]),
    b = new Set([2, 3, 4]),
    c = new Set([0, 1, 2, 3, 4, 5, 6])
const ary = [
    "a",
    "b",
    "c",
    "a.map(x => x * 2)",
    "a.union(b)",
    "a.intersection(b)",
    "a.difference(b)",
    "a.superset_p(c)",
    "a.subset_p(c)",
]
ary.forEach(x => console.log(x, "=>", eval(x)))
実行結果:
a => Set(3) { 0, 1, 2 }
b => Set(3) { 2, 3, 4 }
c => Set(7) { 0, 1, 2, 3, 4, 5, 6 }
a.map(x => x * 2) => Set(3) { 0, 2, 4 }
a.union(b) => Set(5) { 0, 1, 2, 3, 4 }
a.intersection(b) => Set(1) { 2 }
a.difference(b) => Set(2) { 0, 1 }
a.superset_p(c) => false
a.subset_p(c) => true
Set.length
Set.name
Set.prototype
[object]
Setオブジェクトの要素数。