Map
Map objects hold key-value pairs. Any value (object or raw value) can be a key or a value. The constructor Map can take an array as an argument.
The difference between Map and Object
-
An Object key can only be a string or a symbol, but a Map key can be any value.
-
Keys in a Map are ordered (FIFO), while keys added to objects are not.
-
The number of key-value pairs for Map can be obtained from the size property, whereas the number of key-value pairs for Object can only be calculated manually.
-
An Object has its own prototype, and the key name on the prototype chain may conflict with the key name you set on the Object.
- Attributes of the Map object
- Size: Returns the number of key-value pairs contained in the Map object
- Map object method
-
Set (key, val): Adds new elements to the Map
-
Get (key): Finds a specific value by key value and returns it
-
Has (key): checks whether the Map object has the corresponding key value. Returns true if the Map object has the corresponding key value, and false otherwise
-
Delete (key): deletes data from the Map by key value
-
Clear (): Removes all elements from the Map
const m1 = new Map([['a'.111], ['b'.222]])
console.log(m1) // {"a" => 111, "b" => 222}
m1.get('a') / / 111
const m2 = new Map([['c'.3]])
const m3 = new Map(m2)
m3.get('c') / / 3
m3.has('c') // true
m3.set('d'.555)
m3.get('d') / / 555
Copy the code
- Traversal methods
-
Keys () : returns a traverser for key names
-
Values () : Iterator that returns key values
-
Entries () : Returns a traverser for key and value pairs
-
ForEach () : Iterates through each member using the callback function
const map = new Map([['a'.1], ['b'.2]])
for (let key of map.keys()) {
console.log(key)
}
// "a"
// "b"
for (let value of map.values()) {
console.log(value)
}
/ / 1
/ / 2
for (let item of map.entries()) {
console.log(item)
}
// ["a", 1]
// ["b", 2]
/ / or
for (let [key, value] of map.entries()) {
console.log(key, value)
}
// "a" 1
// "b" 2
// for... of... Traversing a map is equivalent to using map.entries()
for (let [key, value] of map) {
console.log(key, value)
}
// "a" 1
// "b" 2
Copy the code
- Map to and from other data structures
- Map to array (using extension operator)
const arr = [[{'a': 1}, 111], ['b': 222]]
const myMap = new Map(arr)
[...myMap] // map to array. [[{'a': 1}, 111], ['b': 222]]
Copy the code
Map
Interchangeability with objects
const obj = {}
const map = new Map(['a'.111], ['b'.222])
for(let [key,value] of map) {
obj[key] = value
}
console.log(obj) // {a:111, b: 222}
Copy the code
JSON
The string is converted toMap
Parse () can be converted to an array or object first, and then converted again.
Set
The Set object allows you to store any type of value, whether primitive or object reference. It is similar to an array, but the values of the members are unique and there are no duplicate values. The Set itself is a constructor used to generate the Set data structure. The Set function can take an array (or some other data structure with an Iterable interface) as an argument for initialization.
Special values in Set
A Set object stores a value that is always unique, so you need to determine whether two values are identical. There are several special values that require special treatment:
-
+0 and -0 are identical in the sense that they store uniqueness, so they are not repeated
-
Undefined and undefined are identical, so they do not repeat
-
NaN is not identical to NaN, but NaN is considered equal to NaN in Set, so there can only be one.
- Set Specifies the properties of the instance object
- Size: Returns the total number of members of the Set instance.
- Set instance object method
-
Add (value) : Adds a value and returns the Set structure itself (which can be called chained).
-
Delete (value) : deletes a value. If the value is deleted successfully, the value is returned true; otherwise, the value is returned false.
-
Has (value) : Returns a Boolean value indicating whether the value is a member of Set.
-
Clear () : Clears all members with no return value.
const mySet = new Set(['a'.'a'.'b'.1.2.1])
console.log(mySet) // {'a', 'b', 1, 2}
myset.add('c').add({'a': 1})
console.log(mySet) // {'a', 'b', 1, 2, 'c', {a: 1}}
console.log(mySet.size) / / 6
mySet.has(2) // true
Copy the code
- Traversal methods
-
Keys () : returns a traverser for key names.
-
Values () : Iterator that returns key values.
-
Entries () : Returns a traverser for key and value pairs.
-
ForEach () : Iterates through each member using the callback function.
Because the Set structure has no key name, only the key value (or the key name and the key value are the same value), the keys and values methods behave exactly the same.
const set = new Set(['a'.'b'.'c'])
for (let item of set.keys()) {
console.log(item)
}
// a
// b
// c
for (let item of set.values()) {
console.log(item)
}
// a
// b
// c
for (let item of set.entries()) {
console.log(item)
}
// ["a", "a"]
// ["b", "b"]
// ["c", "c"]
// Iterate over the set instance directly, which is equivalent to iterate over the values method of the set instance
for (let i of set) {
console.log(i)
}
// a
// b
// c
set.forEach((value, key) = > console.log(key + ':' + value))
// a: a
// b: b
// c: c
Copy the code
- Set object
- Array de-duplication (using extension operators)
const mySet = new Set([1.2.3.4.4])
[...mySet] // [1, 2, 3, 4]
Copy the code
- Merges two sets
let a = new Set([1.2.3])
let b = new Set([4.3.2])
let union = new Set([...a, ...b]) // {1, 2, 3, 4}
Copy the code
- intersection
let a = new Set([1.2.3])
let b = new Set([4.3.2])
let intersect = new Set([...a].filter(x= > b.has(x))) // {2, 3} uses the array filter method
Copy the code
- Difference set
let a = new Set([1.2.3])
let b = new Set([4.3.2])
let difference = new Set([...a].filter(x= >! b.has(x)))// {1}
Copy the code