Preface
The Morning Reading Meeting has been held for three phases so far, have you participated in it? The notes shared today are from Guangzhou @mogeWCY童鞋's notes on one of the chapters of "Real Combat ES2015" in the second issue of Morning Reading.
The text starts here~
Everyone knows that js data types are divided into basic types and reference types
Basic types: number, string, boolean, null, undefined
Reference type: object(Array,Function,Date,Regexp,Error)
But in fact, we may find that when working with languages of other types of work, they will speak out some data types such as dictionaries, lists, and sets. Each Chinese language has different data types, and now ES6 is also catching up
New data structure in ES6
Set
Set is a set of unordered and non-repeating elements (think about the concept of sets in high school mathematics). It is an unordered array, and there are no duplicates in the array. So precisely because of his two characteristics, I thought of these:
You can use this feature to do array deduplication
Because it is unordered, you can't get the elements Ps in the set by index: how to get the value, look down
When creating a collection object, we have to use the new keyword to create it, and we can also pass an array as a parameter to the collection object
Let's take a look at the API of Set
set.add(value): add elements to the collection
set.clear(): Clear the collection object
set.delete(value): delete the element with the value of the collection object
set.forEach(callbackFn[,context]): The same method as the array
set.has(value): Check whether there is an element with value in the collection and return a boolean value. Obviously this method is much more intuitive than the **indexOf** method of the array object
set.values(): Returns a new iterator object that contains the values of all elements in the Set object arranged in the order of insertion
set.keys(): Same as the values() method, returns a new iterator object that contains the values of all elements in the Set object arranged in the order of insertion.
set.size(): returns the number of values of Set objects
Note: Use the following method to quickly achieve array deduplication
WeakSet
WeakSet is an upgraded version of the Set object, which is designed to make reasonable use of memory. The difference from the Set object is roughly as follows:
WeakSet cannot contain value type elements, otherwise a TypeError will be thrown
WeakSet cannot contain unreferenced objects, otherwise the collection will be automatically cleared
The size of WeakSet cannot be detected, and the elements contained in it cannot be detected. The data type of the Weak version cannot contain unreferenced elements. Once the references of any element in the data structure are all removed, the element will be removed The current data structure
Map
The Map object is a simple key/value map
When creating a mapping object, you can directly use the new keyword to create it, or you can pass a binary array (key-value pair) into the construction function, and each key-value pair will be added to the mapping object. The elements in the array will be processed in the order of the array. If the same key exists, the last processed value of the key will be used as the final value according to the first-in-first-out principle.
Let's take a look at the map method
map.set(key,value): Add key-value pairs to the map
map.get(key): Get the corresponding value of a key in the map
map.delete(key): remove a key-value pair from the map
map.clear(): Clear all key-value pairs in the map
map.entries(): returns an array with a binary array (key-value pair) as elements
map.has(key): Check whether a key-value pair is included in the map
map.keys(): returns an iterable object with all keys in the current map as elements
map.values(): returns an iterable object with all values in the current map as elements
map.size(): the number of key-value pairs in the map
The difference between mapping object and Object
The map object can use any object as the key, and it is easy to get the number of key-value pairs. However, these are not possible for ordinary Object objects, pay attention. Object objects have no length property
Finally, Guangzhou @mogeWCY children's shoes participated in the second phase of the morning reading exchange. At that time, the book did not drift to his side, but he used the book he bought to participate in the activity, which is currently the only one.