Bootstrap

javascript Set常用操作

const sets = new Set()
sets.add('hello')
sets.add('hello')
sets.add('kitty')
console.log(sets.size);

for(let word of sets){
    console.log(word);
}
console.log(sets.has('world'));

let arr = [...sets.keys()]
console.log(arr);

sets.forEach((item) => {
    console.log(item);
})

console.log(sets);
sets.delete('hello')
sets.clear()
console.log(sets);

2
hello
kitty
false
[ 'hello', 'kitty' ]
hello
kitty
Set(2) { 'hello', 'kitty' }
Set(0) {}

ts版

const mysets: Set<string> = new Set()
mysets.add('hello')
mysets.add('hello')
mysets.add('kitty')
console.log(mysets.size);

for(let word of mysets){
    console.log(word);
}
console.log(mysets.has('world'));

let myarr: string[] = [...mysets.keys()]
console.log(myarr);

mysets.forEach((item: string) => {
    console.log(item);
})

console.log(mysets);
mysets.delete('hello')
mysets.clear()
console.log(mysets);

;