This article is in need of an editorial review.
This translation is incomplete. Please help translate this article from English.
これは Harmony(ECMAScript 6) 提案の一部であり、実験段階の技術です。
この技術の仕様は安定していません。ブラウザ互換性の一覧表を確認してください。またこれらの構文や動作は、仕様変更などにより、新しいバージョンのブラウザでは変更される可能性があるという点に注意してください。
概要
Set オブジェクトにより、primitive valuesでもオブジェクト参照でも、どんな型でも一意の値を格納します。
構文
new Set([iterable]);
引数
- iterable
- iterable object が渡されたら、その要素すべてが新しいSetオブジェクトに追加されます。nullはundefinedとして扱われます。
説明
Set オブジェクトは値のコレクションです。挿入順に要素を反復することができます。Set内の値は 1度だけ発生します。その値はSetコレクション内で一意です。
値の等価
Setオブジェクト内の各値は一意でなければならないので、値の等価が調べられ、===演算で使われるアルゴリズムと同じアルゴリズムに基いていません。特に、Setに対して、+0 (-0と厳密に言えば等価です) と-0は違った値です。しかしながら、これは最新のECMAScript第6版の仕様では変更されました。 Gecko 29.0 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26) (バグ 952870) や recent nightly Chromeから、+0 と -0はSetオブジェクト内では同じ値として扱われます。また、NaN と undefinedも1つのSet内に格納されます。NaNは、(たとえ NaN !== NaNですが)NaNと同じと考えられます。
プロパティ
Set.lengthlengthの値は0です。get Set[@@species]- 派生オブジェクトを生成するために使用されるコンストラクタ関数。
Set.prototypeSetコンストラクタに対してのプロトタイプを表します。すべてのSetオブジェクトにプロパティを追加できます。
Set インスタンス
すべてのSet インスタンスはSet.prototypeから継承します。
プロパティ
-
Set.prototype.constructor -
Returns the function that created an instance's prototype. This is the
Setfunction by default. -
Set.prototype.size -
Returns the number of values in the
Setobject.
メソッド
-
Set.prototype.add(value) -
Appends a new element with the given value to the
Setobject. Returns theSetobject. -
Set.prototype.clear() -
Removes all elements from the
Setobject. -
Set.prototype.delete(value) -
Removes the element associated to the
valueand returns the value thatSet.prototype.has(value)would have previously returned.Set.prototype.has(value)will returnfalseafterwards. -
Set.prototype.entries() -
Returns a new
Iteratorobject that contains an array of[value, value]for each element in theSetobject, in insertion order. This is kept similar to theMapobject, so that each entry has the same value for its key and value here. -
Set.prototype.forEach(callbackFn[, thisArg]) -
Calls
callbackFnonce for each value present in theSetobject, in insertion order. If athisArgparameter is provided toforEach, it will be used as thethisvalue for each callback. -
Set.prototype.has(value) -
Returns a boolean asserting whether an element is present with the given value in the
Setobject or not. -
Set.prototype.keys() -
Is the same function as the
values()function and returns a newIteratorobject that contains the values for each element in theSetobject in insertion order. -
Set.prototype.values() -
Returns a new
Iteratorobject that contains the values for each element in theSetobject in insertion order. -
Set.prototype[@@iterator]() -
Returns a new
Iteratorobject that contains the values for each element in theSetobject in insertion order.
例
例: Set オブジェクトを使う
var mySet = new Set();
mySet.add(1);
mySet.add(5);
mySet.add("some text");
mySet.has(1); // true
mySet.has(3); // false, 3 has not been added to the set
mySet.has(5); // true
mySet.has(Math.sqrt(25)); // true
mySet.has("Some Text".toLowerCase()); // true
mySet.size; // 3
mySet.delete(5); // removes 5 from the set
mySet.has(5); // false, 5 has been removed
mySet.size; // 2, we just removed one value
例: Setを反復する
// iterate over items in set // logs the items in the order: 1, "some text" for (let item of mySet) console.log(item); // logs the items in the order: 1, "some text" for (let item of mySet.keys()) console.log(item); // logs the items in the order: 1, "some text" for (let item of mySet.values()) console.log(item); // logs the items in the order: 1, "some text" //(key and value are the same here) for (let [key, value] of mySet.entries()) console.log(key); // convert set to plain Array (with Array comprehensions) var myArr = [v for (v of mySet)]; // [1, "some text"] // Alternative (with Array.from) var myArr = Array.from(mySet); // [1, "some text"] // the following will also work if run in an HTML document mySet.add(document.body); mySet.has(document.querySelector("body")); // true // converting between Set and Array mySet2 = new Set([1,2,3,4]); mySet2.size; // 4 [...mySet2]; // [1,2,3,4] // intersect can be simulated via var intersection = new Set([x for (x of set1) if (set2.has(x))]); // Iterate set entries with forEach mySet.forEach(function(value) { console.log(value); }); // 1 // 2 // 3 // 4
例: Array オブジェクトとの関係
var myArray = ["value1", "value2", "value3"];
// Use the regular Set constructor to transform an Array into a Set
var mySet = new Set(myArray);
mySet.has("value1"); // returns true
// Use the spread operator to transform a set into an Array.
alert(uneval([...mySet])); // Will show you exactly the same Array as myArray
仕様
| 仕様 | ステータス | コメント |
|---|---|---|
| ECMAScript 6 (ECMA-262) The definition of 'Set' in that specification. |
勧告候補 | Initial definition. |
ブラウザ実装状況
| 機能 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| 基本サポート |
31 [1] |
13 (13) | 11 | 25 | 7.1 |
コンストラクタ引数t: new Set(iterable) |
38 | 13 (13) | 未サポート | 25 | 未サポート |
| 反復可能 | 38 | 17 (17) | 未サポート | 25 | 7.1 |
Set.clear() |
31 [1] 38 |
19 (19) | 11 | 25 | 7.1 |
Set.keys(), Set.values(), Set.entries() |
37 [1] 38 |
24 (24) | 未サポート | 25 | 7.1 |
Set.forEach() |
36 [1] 38 |
25 (25) | 11 | 25 | 7.1 |
| -0 と 0に対する値の等価 | 34 [1] 38 |
29 (29) | 未サポート | 25 | 未サポート |
コンストラクタ引数: new Set(null) |
(有) | 37 (37) | ? | ? | ? |
モンキーパッチの適用 add() in Constructor |
(有) | 37 (37) | ? | ? | ? |
Set[@@species] |
? | 41 (41) | ? | ? | ? |
| 機能 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| 基本サポート | 未サポート | 31 [1] 38 |
13.0 (13) | 未サポート | 未サポート | iOS 8 |
コンストラクタ引数: new Set(iterable) |
未サポート | 38 | 13.0 (13) | 未サポート | 未サポート | 未サポート |
| 反復可能 | 未サポート | 未サポート | 17.0 (17) | 未サポート | 未サポート | iOS 8 |
Set.clear() |
未サポート | 31 [1] 38 |
19.0 (19) | 未サポート | 未サポート | iOS 8 |
Set.keys(), Set.values(), Set.entries() |
未サポート | 37 [1] 38 |
24.0 (24) | 未サポート | 未サポート | iOS 8 |
Set.forEach() |
未サポート | 36 [1] 38 |
25.0 (25) | 未サポート | 未サポート | iOS 8 |
| -0と0に対する値の等価 | 未サポート | 34 [1] 38 |
29.0 (29) | 未サポート | 未サポート | 未サポート |
コンストラクタ引数: new Set(null) |
? | (有) | 37.0 (37) | ? | ? | ? |
モンキーパッチの適用 add() in Constructor |
? | (有) | 37.0 (37) | ? | ? | ? |
Set[@@species] |
? | ? | 41.0 (41) | ? | ? | ? |
[1] 機能は好みを支持して利用可能です。chrome://flagsでは、 エントリー“Enable Experimental JavaScript”を活性化します。