ES6 New Features 5: Data Structure Example Analysis of SET and MAP

This example describes the data structure of SET and MAP of ES6 new features. Share to everyone for your reference, specifically:

1 Definition: It

is similar to array

, but the value of the member Is unique, no duplicate value. The Set itself is a constructor to generate a set data structure.

VAR S = New set (); [2, 3, 5, 4, 5, 2, 2] .map (x => S.Add (x) ); // set {2, 3, 5, 4}
2 Attributes and methods
  SET structure has the following attributes .  
Set.Prototype.constructor : Constructor, the default is the SET function.
SET.PROTOTYPE.SIZE

: Returns the total number of members of the SET.

The SET data structure has the following method.

Add (value)

: Add a certain value, return the SET structure itself. Delete (Value) : Delete a value, returns a Boolean value, indicating whether deletion is successful.
HAS (Value) : Returns a Boolean value indicating whether the value is a member of Set. CLEAR ()

: Clear all members without returning the value.

VAR S = New set (); S.Add (1) .Add (2) .add (2); // Note 2 has been added two times Console .log (s.size) // 2console.log (s.has (1)) // TrueConsole.log (s.has (2)) // trueconsole.log (s.has (3)) // falseconsole.log (s.delete (2)); console.log (s.has (2)) // False 3 Array.FROM method can transfer SET structures to array

VAR items = new set ([1 , 2, 3, 4, 5]); var arr = array.from (items); // Apply: Remove the method of repeating elements in the array var ARRAY = [1, 2, 3, 2, 3, 4]; function Fun (Array) {Return Array.From (New Set (Array));} console.log (fun (array)); // [1, 2, 3, 4]

4 The SET structure has a VALUES method, returns a traverster.
 VAR S = New set ([1, 2, 3, 4, 5]); console.log (s.values ​​()); // setiterator {1 The default traveler of 2, 3, 4, 5} // set structure is its values ​​method console.log (set.prototype [symbol.Itemrator] === set.prototype.values) // true // So travers You can use for ... Off (Let X of S) {Console.log (x);} // Due to the inner use of the extended operator (...), the set is converted into an array. Var arr = [... s]Console.log (Arr); // [1, 2, 3, 4]   
5 Foreach method for the SET structure

VAR set = new set ([1, 2, 3]); set.Fore, key (value);};
   6 The SET structure also has a Keys and Entries methods, and the key name of each value is the key value. 
Let SET = New Set ([‘Red’, ‘Green’, ‘Blue’]); for (Let Item of Set.keys ()) {Console. Log (item);} // red // Green // BlueFor (let [key, value] of set.entries ()) {console.log (key, value);} // red, red // Green, Green // Blue, Blue

Application of the MAP and Filter method of the array
  MAP (X) {} : Traverse array For each element, the array is returned to the process. 
FILTER (X) {} : Traversing an array, checking each element, returning an array containing the passivity element.

VAR set = new set ([1, 2, 3]); set = new set ([… set] .map (x => x * 2 ); cONSOLE.LOG (SET); // Return to SET Structure: {2, 4, 6} var set = new set ([1, 2, 3, 4, 5]); set = new set ([… set] .filter (x => (x% 2) == 0)); console.log (SET); // Return to SET structure: {2, 4}
   
2. MAP
1 Reason: The object of JavaScript is essentially a collection of key value pairs, but only uses a string as a key.

2 Definition: It is similar to the object, but also a collection of key value pairs, but the range of “key” is not limited to strings, and various types of values ​​(including objects) can be used as a key.
3 Properties and Method
 SIZE : Returns the total number of members.  Set (Key, Value) 
: Set the key value corresponding to the key, and then returns the entire MAP structure. If the key already has a value, the key value will be updated, otherwise the key is newly generated. Get (key)
: Read the key value corresponding to the key, if Key can’t find, returns undefined.

HAS (Key)

: Returns a Boolean value indicating whether a key is in the MAP data structure.

Delete (key) : Delete a key and returns True. Returns false if deleted fails. CLEAR ()
: Clear all members without returning the value. 4 The same can accept the array parameters as the SET to create MAP, but the elements of the array are the array of representative key values.

VAR MAP = New map (); var map1 = new map ([[“” “”, “Zhang San”], [“title “,” Author “]]); console.log (map1.size) // 2
   
⑤ only references to the same object, Map structure before it as the same key, so long as the memory address is not the same, it is considered as two keys

var map = new Map ();. map.set ([ ‘a’ ], 555); console.log (map.get ([ ‘a’])) // undefinedvar k1 = [ ‘a’]; // Although the same value, but this is a new value, the memory address is not the same, the new var k2 = [ ‘a’]; map.set (k1, 111); map.set (k2, 222);

⑥ traverse

to traverse the default interface (Symbol.iterator attributes) the Map structure, method of entries is

keys ()
: returns walker key name values. ()
: returns the key-traverser entries ()
:.. traverser returns all members of the
console. log (Map [Symbol.iterator] === Map.entries) // truelet map = new Map ([[1, ‘one’], [2, ‘two’], [3, ‘three’]]); console.log ([… map.keys ()]); // [1, 2, 3] console.log ([… map.values ​​()]); // [ ‘one’, ‘two ‘,’ three ‘] console.log ([… map.entrIES ()]); //[[1, ‘One’], [2, ‘Two’], [3, ‘Three’]] Console.log ([… map]);[[1, ‘ One ‘], [2,’ TWO ‘], [3,’ Three ‘]]
7map further has a Foreach method for traversal.

8 is the same as the SET, and the MAP can combine the MAP method of the array, the Filter method, can implement MAP traversal and filtering.

It is hoped that this article will help you ECMAScript programming.
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment