{
let list = new Set() //声明定义
list.add(5)
list.add(7)
console.log('size', list.size) //输出结果:size2Set()中添加元素只能用add(),不能重复添加
list.size相当于获取list的长度 == list.length
}
{
let arr = [1,2,3,4,5]
let list = new Set(arr)
console.log('size', list.size) //输出结果:size5
let arr = [1,2,3,1,2]
let list = new Set(arr)
console.log(list) //输出结果:{1,2,3}
let arr = [1,2,3,1,'2']
let list = new Set(arr)
console.log(list) //输出结果:{1,2,3,'2'}
Set()可以在初始化时把元素定义初始化,有去重功能,不会做数据类型的转换
}
{
let arr = ['add','delete','clear','has']
let list = new Set(arr)
console.log('has',list.has('add')) //输出结果:has true
console.log('delete',list.delete('add'),list) //输出结果:deletetrueSet(3) {"delete", "clear", "has"}
list.clear()
console.log('list',list) //输出结果:list Set(0) {}
}
{
let arr = ['add','delete','clear','has']
let list = new Set(arr)
for(let keyof list.keys()){
console.log('keys',key) //输出结果:keys add keys delete keys clear keys has
}
for(let valueof list.values()){
console.log('keys',value) //输出结果:keys add keys delete keys clear keys has
}
for(let valueof list){
console.log('keys',value) //输出结果:keys add keys delete keys clear keys has
}
for(let [key,value] of list.entries()){
console.log('keys',key,value) //输出结果:keys addadd keys deletedelete keys clear clear keys has has
}
list.forEach(function(item){
console.log(item) //输出结果:adddelete clear has
})
}