Set
object lets you store unique values of any type, whether
原语值
or object references.
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
"Key equality for -0 and 0"
在
browser compatibility
table for details.
NaN
and
undefined
can also be stored in a Set. All
NaN
values are equated (i.e.
NaN
is considered the same as
NaN
, even though
NaN !== NaN
).
Set()
Set
对象。
get Set[@@species]
The constructor function that is used to create derived objects.
Set.prototype.size
Set
对象。
Set.prototype.add(
value
)
value
到
Set
object. Returns the
Set
对象。
Set.prototype.clear()
Set
对象。
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.has(
value
)
Set
object or not.
Set.prototype[@@iterator]()
Iterator
object that yields the
值
for each element in the
Set
object in insertion order.
Set.prototype.keys()
Iterator
object that yields the values for each element in the
Set
object in insertion order. (For Sets, this is the same as the
values()
method.)
Set.prototype.values()
Iterator
object that yields the
值
for each element in the
Set
object in insertion order. (For Sets, this is the same as the
keys()
method.)
Set.prototype.entries()
返回新的
Iterator
object that contains
an array of
[
value
,
value
]
for each element in the
Set
object, in insertion order.
This is similar to the
Map
object, so that each entry's
key
is the same as its
value
for a
Set
.
Set.prototype.forEach(
callbackFn
[,
thisArg
])
callbackFn
once for each value present in the
Set
object, in insertion order. If a
thisArg
parameter is provided, it will be used as the
this
value for each invocation of
callbackFn
.
Set
object
let 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' ]
let 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, since 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, since we just removed one value
console.log(mySet)
// Set(4) { 1, "some text", {…}, {…} } in Chrome
// 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
let 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
let intersection = new Set([...set1].filter(x => set2.has(x)))
// difference can be simulated via
let 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
function isSuperset(set, subset) {
for (let elem of subset) {
if (!set.has(elem)) {
return false
}
}
return true
}
function union(setA, setB) {
let _union = new Set(setA)
for (let elem of setB) {
_union.add(elem)
}
return _union
}
function intersection(setA, setB) {
let _intersection = new Set()
for (let elem of setB) {
if (setA.has(elem)) {
_intersection.add(elem)
}
}
return _intersection
}
function symmetricDifference(setA, setB) {
let _difference = new Set(setA)
for (let elem of setB) {
if (_difference.has(elem)) {
_difference.delete(elem)
} else {
_difference.add(elem)
}
}
return _difference
}
function difference(setA, setB) {
let _difference = new Set(setA)
for (let elem of setB) {
_difference.delete(elem)
}
return _difference
}
// Examples
let setA = new Set([1, 2, 3, 4])
let setB = new Set([2, 3])
let setC = new Set([3, 4, 5, 6])
isSuperset(setA, setB) // => true
union(setA, setC) // => Set [1, 2, 3, 4, 5, 6]
intersection(setA, setC) // => Set [3, 4]
symmetricDifference(setA, setC) // => Set [1, 2, 5, 6]
difference(setA, setC) // => Set [1, 2]
Array
对象
let myArray = ['value1', 'value2', 'value3']
// Use the regular Set constructor to transform an Array into a Set
let 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
// Use to remove duplicate elements from the array const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5] console.log([...new Set(numbers)]) // [2, 3, 4, 5, 6, 7, 32]
字符串
let text = 'India'
let mySet = new Set(text) // Set ['I', 'n', 'd', 'i', 'a']
mySet.size // 5
//case sensitive & duplicate ommision
new Set("Firefox") //
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Set' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Set
|
Chrome 38 | Edge 12 | Firefox 13 | IE 11 | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 14 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 |
nodejs
0.12
|
Set()
构造函数
|
Chrome 38 | Edge 12 | Firefox 13 | IE 11 | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 14 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 |
nodejs
0.12
|
add
|
Chrome 38 | Edge 12 | Firefox 13 |
IE
部分支持
11
|
Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 14 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 |
nodejs
0.12
|
clear
|
Chrome 38 | Edge 12 | Firefox 19 | IE 11 | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 19 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
delete
|
Chrome 38 | Edge 12 | Firefox 13 | IE 11 | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 14 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 |
nodejs
0.12
|
entries
|
Chrome 38 | Edge 12 | Firefox 24 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 24 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
forEach
|
Chrome 38 | Edge 12 | Firefox 25 | IE 11 | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
has
|
Chrome 38 | Edge 12 | Firefox 13 | IE 11 | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 14 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 |
nodejs
0.12
|
| Key equality for -0 and 0 | Chrome 38 | Edge 12 | Firefox 29 | IE No | Opera 25 | Safari 9 | WebView Android 38 | Chrome Android 38 | Firefox Android 29 | Opera Android 25 | Safari iOS 9 | Samsung Internet Android 3.0 | nodejs 4.0.0 |
size
|
Chrome 38 | Edge 12 |
Firefox
19
|
IE 11 | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 |
Firefox Android
19
|
Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
值
|
Chrome 38 | Edge 12 | Firefox 24 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 24 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
@@iterator
|
Chrome 43 | Edge 12 |
Firefox
36
|
IE No | Opera 30 | Safari 9 | WebView Android 43 | Chrome Android 43 |
Firefox Android
36
|
Opera Android 30 | Safari iOS 9 | Samsung Internet Android 4.0 | nodejs 0.12 |
@@species
|
Chrome 51 | Edge 13 | Firefox 41 | IE No | Opera 38 | Safari 10 | WebView Android 51 | Chrome Android 51 | Firefox Android 41 | Opera Android 41 | Safari iOS 10 | Samsung Internet Android 5.0 |
nodejs
6.5.0
|
完整支持
部分支持
不支持
见实现注意事项。
用户必须明确启用此特征。
使用非标名称。
Set
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()