Set
Set
?对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用。
语法
new Set([iterable]);
参数
iterable如果传递一个可迭代对象,它的所有元素将被添加到新的?Set中。如果不指定此参数或其值为null,则新的?Set为空。
返回值
一个新的Set
对象。
简述
Set
对象是值的集合,你可以按照插入的顺序迭代它的元素。 Set中的元素只会出现一次,即 Set 中的元素是唯一的。
值的相等
因为 Set 中的值总是唯一的,所以需要判断两个值是否相等。在ECMAScript规范的早期版本中,这不是基于和===操作符中使用的算法相同的算法。具体来说,对于 Set s, +0 (+0 严格相等于-0)和-0是不同的值。然而,在 ECMAScript 2015规范中这点已被更改。有关详细信息,请参阅浏览器兼容性?表中的“value equality for -0 and 0”。
另外,NaN
和undefined
都可以被存储在Set 中,NaN
之间被视为相同的值(尽管 NaN !== NaN)。
属性
Set
实例
所有Set实例继承自 Set.prototype
。
属性
Set.prototype.constructor
返回实例的构造函数。默认情况下是Set
。
Set.prototype.size
返回Set
对象的值的个数。
方法
Set.prototype.add(value)
在Set对象尾部添加一个元素。返回该Set对象。
Set.prototype.clear()
移除Set
对象内的所有元素。
Set.prototype.delete(value)
移除Set的中与这个值相等的元素,返回Set.prototype.has(value)在这个操作前会返回的值(即如果该元素存在,返回true,否则返回false)。Set.prototype.has(value)在此后会返回false。
Set.prototype.entries()
返回一个新的迭代器对象,该对象包含Set对象中的
按插入顺序排列的所有元素的值的[value, value]数组。为了使这个方法
和Map对象保持相似,
?每个值的键和值相等。
Set.prototype.forEach(callbackFn[, thisArg])
按照插入顺序,为Set对象中的每一个值调用一次callBackFn。如果提供了thisArg参数,回调中的this会是这个参数。
Set.prototype.has(value)
返回一个布尔值,表示该值在Set中存在与否。
Set.prototype.keys()
与values()方法相同,返回一个新的迭代器对象,该对象包含Set对象中的
按插入顺序排列的所有元素的值。
Set.prototype.values()
返回一个新的迭代器对象,该对象包含Set对象中的
按插入顺序排列的所有元素的值。
Set.prototype[@@iterator]()
返回一个新的迭代器对象,该对象包含Set对象中的
按插入顺序排列的所有元素的值。
示例
使用Set
对象
var 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}}
迭代Set
// iterate over items in set
// logs the items in the order: 1, "some text", {"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}
for (let item of mySet.keys()) console.log(item);
// logs the items in the order: 1, "some text", {"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}
//(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}]
// 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
?相关
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.
console.log([...mySet]); // Will show you exactly the same Array as myArray
String
?相关
var text = 'Indiana';
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. | Living Standard | ? |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 38 1 | 12 | 13 (13) | 11 | 25 | 7.1 |
Constructor argument: new Set(iterable) | 38 | 12 | 13 (13) | No support | 25 | 9.0 |
Set.add() returns the set | 38 | 12 | 13 (13) | No support | 25 | 7.1 |
Set.clear() | 38 | 12 | 19 (19) | 11 | 25 | 7.1 |
Set.values(), Set.entries() | 38 | 12 | 24 (24) | No support | 25 | 7.1 |
Set.forEach() | 38 | 12 | 25 (25) | 11 | 25 | 7.1 |
Value equality for -0 and 0 | 38 | 12 | 29 (29) | No support | 25 | 9 |
Constructor argument: new Set(null) | (Yes) | 12 | 37 (37) | 11 | (Yes) | 7.1 |
Set@@species | 51 | 13 | 41 (41) | No support | 38 | 10 |
Set() without new throws | (Yes) | 12 | 42 (42) | 11 | (Yes) | 9 |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | No support | 38 1 | (Yes) | 13.0 (13) | No support | No support | 8 |
Constructor argument: new Set(iterable) | No support | 38 | (Yes) | 13.0 (13) | No support | No support | 9 |
Set.clear() | No support | 38 | (Yes) | 19.0 (19) | No support | No support | 8 |
Set.values(), Set.entries() | No support | 38 | (Yes) | 24.0 (24) | No support | No support | 8 |
Set.forEach() | No support | 38 | (Yes) | 25.0 (25) | No support | No support | 8 |
Value equality for -0 and 0 | No support | 38 | (Yes) | 29.0 (29) | No support | No support | 9 |
Constructor argument: new Set(null) | ? | (Yes) | (Yes) | 37.0 (37) | ? | ? | 8 |
Set@@species | ? | ? | (Yes) | 41.0 (41) | ? | ? | 10 |
Set() without new throws | ? | ? | (Yes) | 42.0 (42) | ? | ? | 9 |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com