Today xiaobian and everyone together to discuss the map reference type, in which there will be some map and array joint application, as well as the description of weakMap type similar to Map, this article also adds some methods to operate the array and practical application. You can also follow my wechat public number, Snail Quanzhan.

Declaration of MAP

let s = new Set([1.2.3])
console.log(s) / / Set (3) {1, 2, 3}
// The map does not duplicate the data. If it does, the duplicate elements will be removed. You can apply this feature to de-egalitarian processing of arrays
let s = new Set([1.2.3.1])
console.log(s) / / Set (3) {1, 2, 3}
Copy the code

Add elements to the map using the add method

let s = new Set([1.2.3])
s.add("school")
console.log(s) / / Set (4) {1, 2, 3, "school"}
// You can add elements to a map by chaining them
let s = new Set([1.2.3])
s.add("school").add("bus")
console.log(s) / / Set (5) {1, 2, 3, "school", "bus"}
Copy the code

Delete map elements: use the delete method

let s = new Set([1.2.3])
s.delete(2)
console.log(s) / / Set (2) {1, 3}
Copy the code

Clear all map elements: call clear

let s = new Set([1.2.3])
s.clear()
console.log(s) // Set(0){}
Copy the code

Call the has method to check whether the map contains an element

let s = new Set([1.2.3])
console.log(s.has(2)) // true
Copy the code

Call size to get the number of elements in the map

let s = new Set([1.2.3])
console.log(s.size) / / 3
Copy the code

7. Map traversal

let s = new Set([1.2.3])
s.forEach(item= > console.log(item)) / / 1 2 3

for(let item of s){
    console.log(item) / / 1 2 3
}

for(let item of s.keys()){
    console.log(item) / / 1 2 3
}

for(let item of s.values()){
    console.log(item) / / 1 2 3
}


// For map, key and value are the same
for(let item of s.entries()){
    console.log(item) / / [1, 1] [2] [3, 3]
}
Copy the code

1, array deduplication

let arr = [1.2.3.3.3.2.1]
let s = new Set(arr)
console.log(s) / / Set (3) {1, 2, 3}
console.log([...s]) / / [1, 2, 3]
console.log(Array.from(s)) / / [1, 2, 3]
Copy the code

2. Delete the array after merge

let arr1 = [1.2.3.4]
let arr2 = [2.3.4.5.6]
let s = new Set([...arr1,...arr2])
console.log(s) / / Set (6) 6} {
console.log([...s]) / / [6]
console.log(Array.from(s)) / / [6]
Copy the code

Find the intersection of two arrays

let arr1 = [1.2.3.4]
let arr2 = [2.3.4.5.6]
let s1 = new Set(arr1)
let s2 = new Set(arr2)
let result = new Set(arr1.filter((item) = > s2.has(item)))
console.log(result) / / Set (3) 2 and 4} {
Copy the code

4. Find the difference set of two arrays

let arr1 = [1.2.3.4]
let arr2 = [2.3.4.5.6]
let s1 = new Set(arr1)
let s2 = new Set(arr2)
let s3 = new Set(arr1.filter((item) = >! s2.has(item)))let s4 = new Set(arr2.filter((item) = >! s1.has(item)))console.log(s3) // Map(1){1}
console.log(s4) / / the Map (1) {5, 6}
console.log([...s3,...s4]) / / [1 and 6]
Copy the code

WeakMap: It can only store Object, not other data types

let ws = new WeakSet(a)// ws.add(1) // Error: 1 cannot be added to WeakMap because it is not an Object type
ws.add({
    name:"lilei"
})
ws.add({
    age:12
})
console.log(ws) // WeakSet{{... }, {... }}
Copy the code

1. Delete objects

let ws = new WeakSet(a)Ws.add (1) : Invalid value used in weak set
ws.add({
    name:"lilei"
})
ws.add({
    age:12
})
ws.delete({
    name:"lilei"
})
console.log(ws) // WeakSet{{... }, {... }} The object cannot be deleted because it is a reference data type and the address of the added object is inconsistent with the address of the deleted element
let ws = new WeakSet(a)const obj1 = {name:"lilei"}
const obj2 = {age:12}
ws.add(obj1)
ws.add(obj2)
console.log(ws) // WeakSet{{... }, {... }}

ws.delete(obj1)
console.log(ws) // WeakSet{{... }} {name:"lilei"} has been deleted, mainly because the object is a reference type, directly in the delete write object and the previous object in the heap to point to the same address
console.log(ws.has(obj2)) // true Determines whether there are obj2 objects inside, same as map, using the has function
Copy the code

2, cyclic traversal: JS can not be used for cyclic traversal of WeakMap type data, which is related to garbage collection (GC), the specific mechanism is not clear, in the future article will make up this lesson.

let ws = new WeakSet(a)const obj1 = {name:"lilei"}
const obj2 = {age:12}
ws.add(obj1)
ws.add(obj2)
ws.forEach(item= > console.log(item)) Ws. forEach is not a function
Copy the code