📝 Edit page
➕ Add page
Set
A list of unique elements of any type.
Note that in JS a Set
remembers insertion order, while in other languages like Python, it is unordered.
Resources
Create
const x = new Set()
const x = new Set(["abc", "abc", 123, "def"])
// Set( [ 123, "abc", "def" ] )
Order is preserved and the duplicates from later are removed.
new Set([ 'a', 'b', 'a'])
// Set(2) { 'a', 'b' }
Update
x.add("abc")
x.delete("abc")
Check if present
x.has("abc")
Iterate
for (const item of mySet1) {
console.log(item)
}
(new Set([ 'a', 'b', 'a'])).values()
// [Set Iterator] { 'a', 'b' }
Cast
Convert to array.
const mySet = new Set([ 'a', 'b', 'a'])
[...mySet]
// [ 'a', 'b' ]