JavaScript/Set

出典: フリー教科書『ウィキブックス(Wikibooks)』
JavaScript > 標準ライブラリ Set

Setオブジェクト[編集]

Setオブジェクトは集合を扱うための機能を組み込んだオブジェクトです。 Setオブジェクトの生成には、Setコンストラクターを使います。

const set = new Set()

Setオブジェクトにはリテラルはありません。

コード例[編集]

コード例
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は演算子オーバーロードはできないので集合同士に比較演算子は適用できません。
また、集合演算にも対応しておらず、和集合(union)・積集合(intersection)・差集合(difference)・部分集合か判定(issubset)・上位集合(issuperset)・排他判定(isdisjoint)などのメソッドは用意されていません[1]

Polyfill[編集]

集合演算関係のメソッド追加提案は既にTC39に提出されていますが[2]、参考までに Polyfill を書いてみました。

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
1
コンストラクタの引数の数
Set.name
"Set"
Set.prototype
[object]

静的メソッド[編集]

インスタンスプロパティ[編集]

Set.prototype.size[編集]

Setオブジェクトの要素数。

インスタンスメソッド[編集]

この節は書きかけです。この節を編集してくれる方を心からお待ちしています。

Set.prototype.add(item)[編集]

Set.prototype.clear()[編集]

Set.prototype.constructor()[編集]

Set.prototype.delete(item)[編集]

Set.prototype.entries()[編集]

Set.prototype.forEach(func)[編集]

Set.prototype.has(item)[編集]

Set.prototype.keys()[編集]

Set.prototype.values()[編集]

脚註[編集]

  1. ^ Set - JavaScript // MDN” (2021年12月8日). 2021年12月26日閲覧。
  2. ^ New Set methods” (2019年). 2021年12月26日閲覧。

外部リンク[編集]