Set : Object
Sets are a collection of %%/Object|Objects%% where each
object can only appear once in the set.
Iterable:
true
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-set-objects
----
new Set() : Set
Creates an empty Set.
var x = new Set();
x.add(1);
x.add('a');
x.add({ foo: 'bar' });
for (var item of x) {
console.dir(item);
}
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-set-iterable
----
new Set(iterable : Object) : Set
Creates a Set by iterating over **iterable** and adding
each value to the Set.
var fromArray = new Set([1, '1', { foo: 'bar' }]);
for (var item of fromArray) {
console.dir(item);
}
var generator = function*() {
yield 3;
yield 'javascript';
};
var fromGenerator = new Set(generator());
for (var item of fromGenerator) {
console.dir(item);
}
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-set-iterable
----
instance.size : Number
The number of value in **this**.
var x = new Set(['a', 'b', 'c']);
console.log(x.size);
x.add('d');
console.log(x.size);
x.clear();
console.log(x.size);
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-get-set.prototype.size
----
prototype.add(value : Object) : Set
Stores **value** in **this**. If **value** is already stored,
there is no change to the Set.
Returns **this**.
var x = new Set(['a', 'b', 'c']);
console.log(x.has('a'));
console.log(x.has('d'));
x.add('d');
console.log(x.has('d'));
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-set.prototype.add
----
prototype.clear() : undefined
Clears all values from **this**.
var x = new Set(['a', 'b', 'c']);
console.log(x.size);
x.clear();
console.log(x.size);
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-set.prototype.clear
----
prototype.delete(value : Object) : Boolean
Removes **value* from **this**.
Returns **true** if **value** was in **this** before deleting it.
var x = new Set(['a', 'b', 'c']);
console.log('size before delete: ' + x.size);
console.log('deleted "a" = ' + x.delete('a'));
console.log('size after delete: ' + x.size);
console.log('deleted "foo" = ' + x.delete('foo'));
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-set.prototype.delete
----
prototype.entries() : Iterator
Returns an iterator of the items in **this** where the
%%Iterator#next|**values**s%% of the iterator are of the form
**[value : %%/Object|Object%%, value : %%/Object|Object%%]**
(**value** is duplicated twice).
See also %%#keys|**keys()**%% and %%#values|**values()**%%.
var x = new Set(['a', 'b', 'c']);
// Use for (... of ...) to loop over the iterator
for (var entry of x.entries()) {
console.dir(entry);
}
// Or access the iterator manually
var entries = x.entries();
console.dir(entries.next());
console.dir(entries.next());
console.dir(entries.next());
console.dir(entries.next());
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-set.prototype.entries
----
prototype.forEach(callback(value : Object, key : Object, set : Set) : undefined, [thisArg : Object]) : undefined
Calls **callback** for each value in **this**.
The **Set** passed to **callback** is the **this** of the call to **forEach**.
var x = new Set(['a', 'b', 'c']);
x.forEach(function(value) {
console.dir(value);
});
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-set.prototype.forEach
----
prototype.has(value : Object) : Boolean
Returns **true** if the set contains **value**.
var x = new Set(['a', 'b', 'c']);
console.log(x.has('a'));
console.log(x.has('foo'));
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-set.prototype.has
----
prototype.keys() : Iterator
Same as %%#values|values()%%.
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-set.prototype.keys
----
prototype.values() : Iterator
Returns an iterator of the values in **this**.
The **values** function is also returned for
**this[%%/Symbol#iterator|Symbol.iterator%%]** so you can
iterate over **this** directly to get the values.
See also %%#entries|**entries()**%% and %%#keys|**keys()**%%.
var x = new Set(['a', 'b', 'c']);
// values is returned by x[Symbol.iterator] so you can just
// use x directly in the for (... of ...) loop
for (var value of x) {
console.dir(value);
}
// You can iterate over values explicitly
for (var value of x.values()) {
console.dir(value);
}
// Or access the iterator manually
var values = x.values();
console.dir(values.next());
console.dir(values.next());
console.dir(values.next());
console.dir(values.next());
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-set.prototype.values