A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Set/intersection below:

Set.prototype.intersection() - JavaScript | MDN

Set.prototype.intersection()

Baseline 2024

Newly available

intersection() は Set インスタンスのメソッドで、集合を受け取り、この Set と指定された集合の両方にある要素の入った新しい Set を返します。

構文 引数
other

Set オブジェクト、または Set 風オブジェクトです。

返値

新しい Set オブジェクトで、この Set と other の集合の両方にある要素が入ったものです。

解説

数学的な記法では、交差 (intersection) は次のように定義されます。

A ∩ B = { x ∊ A ∣ x ∊ B } A\cap B = \{x\in A\mid x\in B\}

ベン図を使うとこうなります。

intersection() は、Set 風オブジェクトを other 引数として受け入れます。this は、ユーザーコードを呼び出すことなく、this オブジェクトに格納されているデータに直接アクセスするため、実際の Set インスタンスであることが要求されます。その後、その動作は this と other のサイズに依存します。

この実装により、intersection() の実行効率は、this と other のうち小さい方のセットのサイズにほぼ依存します(Set が指数時間でアクセスできると仮定した場合)。返される Set の要素の順序は、this と other のうち小さい方のセットの順序と同じです。

例 intersection() の使用

次の例では、奇数(10 未満)と完全平方(10 未満)の集合の共通部分を計算します。結果は、完全平方である奇数の集合となります。

const odds = new Set([1, 3, 5, 7, 9]);
const squares = new Set([1, 4, 9]);
console.log(odds.intersection(squares)); // Set(2) { 1, 9 }
仕様書 ブラウザーの互換性 関連情報

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4