This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
preface
- You learned about the new ES6 functions in the previous article
- This article introduces two new data structures called Set Map
Set
-
A Set is a data structure for a collection
- A collection of disordered, related, and non-repeating elements
- Like an array, all elements have unique values
- The Set itself is the constructor used to generate the Set data structure
let a = new Set(a)Copy the code
- Set add delete change check method
add
Adding a value returns the Set itself
let a = new Set() a.add(1).add(1) // 1 is added only once. Set does not allow duplicate elements Copy the code
delete
Deleting an element returns a Boolean value
let a = new Set() a.add(1).add(2) / / {1, 2} a.delete(1) // true a / / {2} Copy the code
has
Returns a Boolean value that checks whether the element exists in the set
let a = new Set() a.add(1) / / {1} a.has(1) // true Copy the code
clear
Clears all elements with no return value
let a = new Set() a.add(1).add(3) / / {1, 3} a.clear() a / / {} Copy the code
-
Set traversal method
keys
Returns a traverser for the key name
let a = new Set([1.2.3]) for(let item of a.keys) { console.log(item) / / 1 2 3 } Copy the code
values
Returns a traverser for key values
let a = new Set([1.2.3]) for(let item of a.values) { console.log(item) / / 1 2 3 } Copy the code
entries
Returns a traverser for key-value pairs
let a = new Set([1.2.3]) for(let item of a.values) { console.log(item) / / [1, 1] [2] [3, 3] } Copy the code
forEach
Use the callback function to iterate over each element
let a = new Set([1.2.3]) a.forEach((value,key) = >{console.log(`${key}:${value}`)}) / / 1:1 2:2 3-3 Copy the code
- The extension operator and set are used to implement array de-duplication
let arr = [1.2.3.3.2.1] let arr2 = [...new Set(arr)] / / [1, 2, 3] Copy the code
Map
-
An ordered list of key-value pairs keys and values can be of any type
- The map itself is the constructor used to generate the map structure
let map = new Map(a)Copy the code
-
Add, delete, modify, and check map
- Size Returns the number of elements in the map structure
let map = new Map() map.set(1.2) map.size / / 1 Copy the code
- Set sets the key name and key value and returns map overridden if the key name is the same
let map = new Map() map.set(1.2).set(3.4) // use the chaining method Copy the code
- Get Reads the value of the key and returns undefined if the key is not found
let map = new Map() map.set(1.2).set(3.4) / / {1 = > 2, 3 = > 4} map.get(1) / / 2 Copy the code
- Has finds whether a key returns a Boolean value in the current map
let map = new Map() map.set(1.2) / / 1 = > {2} map.has(1) //true Copy the code
- Delete Deletes a key and returns a Boolean value
let map = new Map() map.set(1.2).set(3.4) / / {1 = > 2, 3 = > 4} map.delete(1) //true map / / 3 = > {4} Copy the code
- Clear Clears all elements with no return value
let map = new Map() map.set(1.2).set(3.4) / / {1 = > 2, 3 = > 4} map.clear map / / {} Copy the code
-
The Map of traverse
- Keys returns a traverser for the key name
let map = new Map([['a'.1], ['b'.2]])for(let item of map.keys()) { console.log(item) // 'a' 'b' } Copy the code
- Values returns a traverser for key values
let map = new Map([['a'.1], ['b'.2]])for(let item of map.values()) { console.log(item) / / 1. 2 } Copy the code
- Entries return a traverser for key – value pairs
let map = new Map([['a'.1], ['b'.2]])for(let item of map.entries()) { console.log(item) // ['a',1] ['b',1] } Copy the code
- ForEach traverses all elements of the map
let map = new Map([['a'.1], ['b'.2] ]) map.forEach((value,key) = >{ console.log(key,value) }) // a 1 b 2 Copy the code
The difference between Set and Map
- In common
- Collections and dictionaries can store values that are not duplicated
- The difference between
- Collections store elements as [values, values] and dictionaries as [keys, values]
conclusion
- Today’s lesson
- “Feel free to discuss in the comments section, nuggets officials will be onProject DiggAfter the event, 100 nuggets will be selected in the comments section. Details can be found in the event article.