Set<T>
SetはJavaScriptの組み込みAPIのひとつで、値のコレクションを扱うためのオブジェクトです。Setには重複する値が格納できません。Setに格納された値は一意(unique)になります。
Setオブジェクトの作り方
Setオブジェクトを新たに作るには、Setクラスをnewします。コンストラクタに配列を渡すと、値がSetに格納されます。
tsconstfruits = newSet (["apple", "orange", "banana"]);console .log (fruits );
tsconstfruits = newSet (["apple", "orange", "banana"]);console .log (fruits );
コンストラクタに渡す配列の中に重複がある場合、重複した値は取り除かれます。
tsconstfruits = newSet (["apple", "apple", "apple"]);console .log (fruits );
tsconstfruits = newSet (["apple", "apple", "apple"]);console .log (fruits );
コンストラクタ引数を省略した場合、空のSetオブジェクトが作られます。
tsconstfruits = newSet ();console .log (fruits );
tsconstfruits = newSet ();console .log (fruits );
空のSetオブジェクトのTypeScript上の型はSet<unknown>になります。これでは後からSetに値を追加できないので、空のSetを作るときは、Setの型変数を指定する必要があります。
tsconst fruits = new Set<string>();// ^^^^^^^^ 型変数を指定
tsconst fruits = new Set<string>();// ^^^^^^^^ 型変数を指定
Setの型注釈
TypeScriptでSetの型注釈をする場合は、Set<string>のようにSet要素の型を型変数に指定します。
tsfunctiondoSomething (strings :Set <string>) {// ...}
tsfunctiondoSomething (strings :Set <string>) {// ...}
Setの操作
値を追加する - Set.prototype.add()
Setに値を追加するにはaddメソッドを用います。同じ値は何度追加しても増えないようになっています。
tsconstfruits = newSet <string>();fruits .add ("apple");fruits .add ("apple");console .log (fruits );
tsconstfruits = newSet <string>();fruits .add ("apple");fruits .add ("apple");console .log (fruits );
追加した値は最後に足されます。すでに存在する値は、追加されず順番は変わりません。
tsconstnumbers = newSet <number>();numbers .add (1).add (2).add (3);numbers .add (1);console .log (numbers );
tsconstnumbers = newSet <number>();numbers .add (1).add (2).add (3);numbers .add (1);console .log (numbers );
値を削除する - Set.prototype.delete()
Setから値を取り除くには、deleteメソッドを使います。
tsconstnumbers = newSet ([1, 2, 3]);numbers .delete (3);console .log (numbers );
tsconstnumbers = newSet ([1, 2, 3]);numbers .delete (3);console .log (numbers );
値の有無を確認する - Set.prototype.has()
Setに値が存在するかどうかはhasメソッドで調べられます。
tsconstnumbers = newSet ([1, 2, 3]);console .log (numbers .has (1));console .log (numbers .has (999));
tsconstnumbers = newSet ([1, 2, 3]);console .log (numbers .has (1));console .log (numbers .has (999));
値の個数を取得する - Set.prototype.size()
Setにいくつ値が登録されているかを調べるには、sizeフィールドの値を見ます。
tsconstfruits = newSet (["apple", "orange", "banana"]);console .log (fruits .size );
tsconstfruits = newSet (["apple", "orange", "banana"]);console .log (fruits .size );
Setを空っぽにする - Set.prototype.clear()
Setに登録された値をすべて削除するにはclearメソッドを使います。
tsconstfruits = newSet (["apple", "orange", "banana"]);fruits .clear ();console .log (fruits );
tsconstfruits = newSet (["apple", "orange", "banana"]);fruits .clear ();console .log (fruits );
Setをループする
Setオブジェクトはfor-of構文でループできます。
tsconstfruits = newSet (["apple", "orange", "banana"]);for (constfruit offruits ) {console .log (fruit ); // "apple"、"orange"、"banana"の順で出力される}
tsconstfruits = newSet (["apple", "orange", "banana"]);for (constfruit offruits ) {console .log (fruit ); // "apple"、"orange"、"banana"の順で出力される}
📄️ for-of文 - 拡張for文
JavaScriptで配列をループするのに使えるのがfor-of構文です。PHPのforeachやPythonのfor inと使い勝手が似ている構文です。
Setを配列に変換する
Setオブジェクトを配列に変換するには、スプレッド構文を用います。
tsconstfruits = newSet (["apple", "orange", "banana"]);constarray = [...fruits ];console .log (array );
tsconstfruits = newSet (["apple", "orange", "banana"]);constarray = [...fruits ];console .log (array );
📄️ 配列のスプレッド構文「...」
JavaScript の配列ではスプレッド構文「...」を使うことで、要素を展開することができます。
Setは直接JSONにできない
SetオブジェクトはJSON.stringifyにかけても、Setに登録されている値はJSONになりません。
tsconstfruits = newSet (["apple", "orange", "banana"]);console .log (JSON .stringify (fruits ));
tsconstfruits = newSet (["apple", "orange", "banana"]);console .log (JSON .stringify (fruits ));
SetオブジェクトのデータをJSON化したい場合は、一度配列にするなどひと手間必要です。
tsconstfruits = newSet (["apple", "orange", "banana"]);constarray = [...fruits ];console .log (JSON .stringify (array ));
tsconstfruits = newSet (["apple", "orange", "banana"]);constarray = [...fruits ];console .log (JSON .stringify (array ));
レシピ
配列から重複要素を取り除く
「Setに渡した値は重複しない」という特性を使って、配列から値が重複する要素を取り除く処理に応用できます。
jsconstarray1 = [0, 0, 1, 1, 2, 2];constarray2 = [...newSet (array1 )];console .log (array2 );
jsconstarray1 = [0, 0, 1, 1, 2, 2];constarray2 = [...newSet (array1 )];console .log (array2 );