JS-ES6 Learning Notes-set structure and map structure

Source: Internet
Author: User
Tags map data structure

1. ES6 provides a new Set of data structures. It is similar to an array, but the values of the members are unique and have no duplicate values.

The set itself is a constructor that is used to generate a set data structure.

2. The Set function can accept an array (or an array-like object) as an argument for initialization.

3 Set.prototype.size : Returns Set the total number of members of the instance.

Four methods of operation.

    • add(value): Adds a value that returns the set structure itself.
    • delete(value): Deletes a value that returns a Boolean value indicating whether the deletion was successful.
    • has(value): Returns a Boolean value that indicates whether the value is a Set member.
    • clear(): Clears all members without a return value.

4, the set structure of the instance has four traversal methods, can be used to traverse the members.

    • keys(): The iterator that returns the key name
    • values(): The iterator that returns the key value
    • entries(): A walker that returns a key-value pair
    • forEach(): Use the callback function to traverse each member

Because the SET structure does not have a key name, only the key value (or the key name and key value is the same value), so keys the methods and values methods behave exactly the same.

entriesmethod, including the key name and key value, so each time an array is output, its two members are exactly equal.

You can iterate for...of through the set with loops.

5. The extension operator ( ... ) uses for...of loops internally, so it can also be used for set structures.

Let set = new set ([' Red ', ' green ', ' blue '); let arr = [... set];//[' Red ', ' green ', ' blue ']

6. The Union, intersection (Intersect), and Difference set (difference) can be easily implemented by using set.

Let-a = new set ([1, 2, 3]); let B = new set ([4, 3, 2]);//and set let union = new Set ([... A, ... b]);//set {1, 2, 3, 4}//intersection let I Ntersect = new Set ([... a].filter (x = B.has (x)));//set {2, 3}//difference set let difference = new set ([...] a].filter (x =!b.ha s (x)));//Set {1}

7. The weakset structure is similar to set and is also a collection of values that are not duplicates. However, it has two differences from set.

First, the members of the Weakset can only be objects, not other types of values.

Second, the object in Weakset is a weak reference, that is, the garbage collection mechanism does not consider the Weakset reference to the object, that is, if the object is no longer referenced by other objects, the garbage collection mechanism automatically reclaims the memory occupied by the object, regardless of whether the object exists in Weakset. This feature means that Weakset members cannot be referenced, so weakset is not traversed.

8. One use of Weakset is to store DOM nodes without worrying about a memory leak when these nodes are removed from the document.

9, JavaScript objects (object), is essentially a set of key-value pairs (hash structure), but traditionally can only use a string as a key. This has brought a great limit to its use.

ES6 provides a map data structure. It is similar to an object and a collection of key-value pairs, but the scope of the key is not limited to strings, and the various types of values (including objects) can be treated as keys. That is, the object structure provides a "string-value" correspondence, the map structure provides "value-value" correspondence, is a more perfect hash structure implementation. If you need a "key-value pair" data structure, map is more appropriate than object.

10, the key of the map is actually bound with the memory address, as long as the memory address is not the same, it is treated as two keys. This solves the problem of property collisions with the same name (clash), when we extend someone else's library, if we use the object as the key name, we don't have to worry about our property having the same name as the original author's property.

11, combined with the method of the array, map filter method, you can implement the map traversal and filtering (map itself does not map and filter methods).

Let map0 = new map (). Set (  1, ' a '). Set (2, ' B ').  Set (  3, ' C '); let Map1 = new map ([  ... map0].filter ([k, V]) + K < 3);//Generate map Structure {1 = ' a ', 2 = ' B '}let map2 = new Map (  [... map0].map ([k, v]) = = [k * 2, ' _ ' + V ])    ;//Generate map Structure {2 = ' _a ', 4 = ' _b ', 6 = ' _c '}

12, WeakMap structure and Map structure basically similar, the only difference is that it only accepts the object as the key name ( null except), does not accept other types of values as the key name, and the key name points to the object, not counted into the garbage collection mechanism.

A typical application is a structure that corresponds to a DOM element, and WeakMap when a DOM element is cleared, its corresponding WeakMap record is automatically removed. Basically, WeakMap the special occasion is that its keys correspond to the object that may disappear in the future. WeakMapstructure helps prevent memory leaks.

Transferred from: http://www.cnblogs.com/zczhangcui/p/6441385.html

JS-ES6 Learning Notes-set structure and map structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.