Set and Map are two new data structures for collection provided by ES6. Although other traditional programming languages like java, php, have provide Set and Map from the beginning, EMCAScript also tend to absorb their advantages and implement more features that itself didn’t provide.
Set
Set is a kind of collections whose containing elements are unique, which means that if adding the same elements into a set, it will just store one and neglect the others. The criteria for equality in a set is almost the same to strict equality ===
, except that NaN
is considering to equal to itself, which is different from ===
.
// create a new Set
let set = new Set()
// create a new Set from array or set
let set1 = new Set([1, 2, 1])
let set2 = new Set(set1)
console.log(set1)
// 1 2
// Set provides property .size and methods .add(), .delete(), .has() and .clear() for element operations
let set = new Set()
set.add(1) // you could just add one element at a time
set.add(1) // do nothing because the set contains the element
set.has(1) // return true if the set containing the element
set.add(2)
set.delete(1) // delete the element, it will do nothing if the element does not exist
set.clear() // clear all elements in the set
// Set provides methods .keys() .values(), .entries() and .forEach() for iteration. These methods return a iterator, which you could not use .add() or .delete() methods.
// Since Set does not have key, so .keys(), .values() are the same. .entries() return iterator that contains elements in set as key and value in an array
let set = new Set([0, 1, 2])
for (let key of set.keys()) {
console.log(key)
}
for (let value of set.values()) {
console.log(value)
}
for (let entry of set.entries()) {
console.log(entry)
}
set.forEach((value) => {
console.log(value)
})
// 0 1 2
// 0 1 2
// [0, 0] [1, 1] [2, 2]
// 0 1 2
// default iterator is .values()
for (let value of set) {
console.log(value)
}
// 0 1 2
Map
Map is a kind of kay-value collection. It provides hash method to directly mapping from key to memory address, so it’s efficient for data look-up. In ES5, we implement “mapping” function with object, but that has a drawback: the key in Object could just be String. If the key is Number or Object, it will be converted to String with toString()
. In ES6, any data type could be a key in map. Map could anything to a memory address for storage.
// create a new Map
let map = new Map()
// create a new Map with an array
let map1 = new Map([['key1', 'value1'], ['key2', 'value2']])
// Map has .size property and .get(), .set(), .delete(), .has(), .clear() methods for operations
map.set('name', 'name')
// .set() return the map itself, so chain rule is applicable
map.set('key1', 'value1').set('key2', 'value2')
map.get('name') // 'name', if not exist, return undefined
map.has('name') // true
map.delete('name') // delete the key and return true, if the key does not exist, do nothing and return false
map.clear() // reset the map
// Map provides .keys(), .values(), .entries() and .forEach() for iteration
let map = new Map([['key1', 'value1'], ['key2', 'value2']])
for (let key of map.keys()) {
console.log(key)
}
// key1 key2
for (let value of map.values()) {
console.log(value)
}
// value1 value2
for (let entry of map.entries()) {
console.log(entry[0], entry[1])
}
// key1, value1
// key2, value2
// the same as to
for (let [key, value] of map.entries()) {
console.log(key, value)
}
// the same as to
for (let [key, value] of map) {
console.log(key, value)
}
WeakSet And WeakMap
WeakSet is a special kind of Set whose element must be object, and the object it contains could be destroyed if no other reference(except the one in WeakSet).
WeakMap is similar to Map too, except that its keys must be object and also the object it refer to might be destroyed if no other reference.