The Set
object lets you store unique values of any type, whether primitive values or object references.
new Set([iterable]);
iterable
Set
. If you don't specify this parameter, or its value is null
, the new Set
is empty.A new Set
object.
Set
objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set
may only occur once; it is unique in the Set
's collection.
Because each value in the Set has to be unique, the value equality will be checked. In an earlier version of ECMAScript specification, this was not based on the same algorithm as the one used in the === operator. Specifically, for Set
s, +0
(which is strictly equal to -0
) and -0
were different values. However, this was changed in the ECMAScript 2015 specification. See "Value equality for -0 and 0" in the browser compatibility table for details.
Also, NaN
and undefined
can also be stored in a Set. NaN
is considered the same as NaN
(even though NaN !== NaN
).
Set.length
length
property is 0.get Set[@@species]
Set.prototype
Set
constructor. Allows the addition of properties to all Set
objects.Set
instancesAll Set
instances inherit from Set.prototype
.
Set.prototype.constructor
Set
function by default.Set.prototype.size
Set
object.Set.prototype.add(value)
Set
object. Returns the Set
object.Set.prototype.clear()
Set
object.Set.prototype.delete(value)
value
and returns the value that Set.prototype.has(value)
would have previously returned. Set.prototype.has(value)
will return false
afterwards.Set.prototype.entries()
Iterator
object that contains[value, value]
for each element in the Set
object, in insertion order. This is kept similar to the Map
object, so that each entry has the same value for its key and value here.Set.prototype.forEach(callbackFn[, thisArg])
callbackFn
once for each value present in the Set
object, in insertion order. If a thisArg
parameter is provided to forEach
, it will be used as the this
value for each callback.Set.prototype.has(value)
Set
object or not.Set.prototype.keys()
values()
function and returns a new Iterator
object that contains the values for each element in the Set
object in insertion order.Set.prototype.values()
Iterator
object that contains the values for each element in the Set
object in insertion order.Set.prototype[@@iterator]()
Iterator
object that contains the values for each element in the Set
object in insertion order.Set
objectvar mySet = new Set(); mySet.add(1); // Set { 1 } mySet.add(5); // Set { 1, 5 } mySet.add(5); // Set { 1, 5 } mySet.add('some text'); // Set { 1, 5, 'some text' } var o = {a: 1, b: 2}; mySet.add(o); mySet.add({a: 1, b: 2}); // o is referencing a different object so this is okay 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.has(o); // true mySet.size; // 5 mySet.delete(5); // removes 5 from the set mySet.has(5); // false, 5 has been removed mySet.size; // 4, we just removed one value console.log(mySet);// Set {1, "some text", Object {a: 1, b: 2}, Object {a: 1, b: 2}}
// iterate over items in set // logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} for (let item of mySet) console.log(item); // logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} for (let item of mySet.keys()) console.log(item); // logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} for (let item of mySet.values()) console.log(item); // logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} //(key and value are the same here) for (let [key, value] of mySet.entries()) console.log(key); // convert Set object to an Array object, with Array.from var myArr = Array.from(mySet); // [1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}] // 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([...set1].filter(x => set2.has(x))); // difference can be simulated via var difference = new Set([...set1].filter(x => !set2.has(x))); // Iterate set entries with forEach mySet.forEach(function(value) { console.log(value); }); // 1 // 2 // 3 // 4
Set.prototype.isSuperset = function(subset) { for (var elem of subset) { if (!this.has(elem)) { return false; } } return true; } Set.prototype.union = function(setB) { var union = new Set(this); for (var elem of setB) { union.add(elem); } return union; } Set.prototype.intersection = function(setB) { var intersection = new Set(); for (var elem of setB) { if (this.has(elem)) { intersection.add(elem); } } return intersection; } Set.prototype.difference = function(setB) { var difference = new Set(this); for (var elem of setB) { difference.delete(elem); } return difference; } //Examples var setA = new Set([1, 2, 3, 4]), setB = new Set([2, 3]), setC = new Set([3, 4, 5, 6]); setA.isSuperset(setB); // => true setA.union(setC); // => Set [1, 2, 3, 4, 5, 6] setA.intersection(setC); // => Set [3, 4] setA.difference(setC); // => Set [1, 2]
Array
objectsvar 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. console.log([...mySet]); // Will show you exactly the same Array as myArray
Strings
var text = 'India'; var mySet = new Set(text); // Set {'I', 'n', 'd', 'i', 'a'} mySet.size; // 5
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Set' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of 'Set' in that specification. | Draft |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 38 | 12 | 13 | 11 | 25 | 8 |
new Set(iterable) |
38 | 12 | 13 | No | 25 | 9 |
new Set(null) |
Yes | 12 | 37 | 11 | Yes | 9 |
Set() without new throws |
Yes | 12 | 42 | 11 | Yes | 9 |
Key equality for -0 and 0 | 38 | 12 | 29 | No | 25 | 9 |
add |
38 | 12 | 13 | 111 | 25 | 8 |
clear |
38 | 12 | 19 | 11 | 25 | 8 |
delete |
38 | 12 | 13 | 11 | 25 | 8 |
entries |
38 | 12 | 24 | No | 25 | 8 |
forEach |
38 | 12 | 25 | 11 | 25 | 8 |
has |
38 | 12 | 13 | 11 | 25 | 8 |
prototype |
38 | 12 | 13 | 11 | 25 | 8 |
size |
38 | 12 | 192 | 11 | 25 | 8 |
values |
38 | 12 | 24 | No | 25 | 8 |
@@iterator |
Yes | Yes |
17 — 273 366 |
No | Yes | Yes |
@@species |
51 | 13 | 41 | No | 38 | 10 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | 38 | 38 | 12 | 14 | 25 | 8 | ? |
new Set(iterable) |
38 | 38 | 12 | 14 | 25 | 9 | ? |
new Set(null) |
Yes | Yes | 12 | 37 | Yes | 9 | ? |
Set() without new throws |
Yes | Yes | 12 | 42 | Yes | 9 | ? |
Key equality for -0 and 0 | 38 | 38 | 12 | 29 | 25 | 9 | ? |
add |
38 | 38 | 12 | 14 | 25 | 8 | ? |
clear |
38 | 38 | 12 | 19 | 25 | 8 | ? |
delete |
38 | 38 | 12 | 14 | 25 | 8 | ? |
entries |
38 | 38 | 12 | 24 | 25 | 8 | ? |
forEach |
38 | 38 | 12 | 25 | 25 | 8 | ? |
has |
38 | 38 | 12 | 14 | 25 | 8 | ? |
prototype |
38 | 38 | 12 | 14 | 25 | 8 | ? |
size |
38 | 38 | 12 | 192 | 25 | 8 | ? |
values |
38 | 38 | 12 | 24 | 25 | 8 | ? |
@@iterator |
Yes | Yes | Yes |
17 — 273 366 |
Yes | Yes | ? |
@@species |
51 | 51 | 13 | 41 | 38 | 10 | ? |
1. Returns 'undefined' instead of the 'Set' object.
2. From Firefox 13 to Firefox 18, the size
property was implemented as a Set.prototype.size()
method, this has been changed to a property in later versions conform to the ECMAScript 2015 specification.
3. Supported as iterator
.
4. A placeholder property named @@iterator
is used.
5. Supported as @@iterator
.
6. The @@iterator
symbol is implemented.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set