Set
构造函数能让你创建 Set
对象,其可以存储任意类型的唯一值,无论是 primitive values 或者对象引用。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
new Set([iterable])
Parameters
iterable
可选- 如果传递一个可迭代对象,它的所有元素将不重复地被添加到新的 Set中。
- 如果不指定此参数或其值为
null
,则新的 Set为空。
Return value
A new Set
object.
Examples
Using the 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)
Specifications
Specification |
---|
ECMAScript (ECMA-262) Set constructor |
Browser compatibility
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.