ES6 's data structure set and map

Source: Internet
Author: User
Tags map data structure new set
ES6 provides us with two data structures set and map, which are sometimes convenient for us to process our data. The two methods are analyzed directly. One, set data structure

Set itself is a constructor that is used to generate set data structures, similar to arrays, but whose values are unique.
The set internally determines whether two values are different, similar to the exact equality operator (= = =), the main difference being that nan equals itself, whereas the exact equality operator thinks Nan is not equal to itself, and two objects are always unequal.

1.set usage (excluding duplicate data)

var ary = [1,2,3,4,4,3,2,1];
 Let set = new set (ary);
 Console.log (SET)  //[1,2,3,4]

Properties of 2.set
①set.prototype.constructor Set's constructor defaults to its own
②set.prototype.size returns the total number of instance members. For example, in the top code

Set.size  //back to 4

3.set similar to the array of some additions and deletions to check the method

Add: Adds a value that returns the set structure itself.

Delete (value): Deletes a value, returns a Boolean value, indicating whether the deletion was successful.

Has (value): Returns a Boolean value that indicates whether the value is a member of set.

Clear (): Clears all Members, no return value.

You can use For...of. To traverse the set inner element

For example:

ary = ["A", "B", "C", "D"] let
set = new set (ary);
Set.add ("E")//["A", "B", "C", "D", "E"]
set.delete ("a")//["B", "C", "D", "E"]
Set.has ("B")//True
Set.clear ()//[]

Use For...of. To traverse the set inner element

var ary = ["A", "B", "C", "D"];
 Let set = new set (ary);
   For (A of set) {
      Console.log (a)
   }
//result: a b c D

4. Some traversal operations on the data (not just set, for example, map is also available)

Keys (): Returns an iterator for a key name

VALUES (): An iterator that returns a key value

Entries (): Returns an iterator for a key-value pair

ForEach (): Use callback function to traverse each member

First, the key names and values in the set data are the same, so the keys () method and the values () method have the same effect on the set.

var ary = ["A", "B", "C", "D"];
Let set = new set (ary);
Console.log (Set.keys ())//setiterator {"A", "B", "C", "D"}
Console.log (Set.values ())//setiterator {"A", "B", "C", "D"}
Console.log (Set.entries ())//setiterator {["A", "a"], ["B", "B"], ["C", "C"], ["D", "D"]}

ForEach (): Use callback function to traverse each member (same as Array)

var ary = ["A", "B", "C", "D"];
 Let set = new set (ary);
 Set.foreach (function (key,value,obj) {
   console.log (key)//Traversal key
   Console.log (value)//traversal value (although set's keys and values are the same)
   Console.log (obj)//Return traversal object
 })

ES6 also provides a way to extend an array ... can also be used for set
For example:

var ary = ["A", "B", "C", "D"];
 Let set = new set (ary);
 Console.log (set)  //set {"A", "B", "C", "D"}
 console.log ([... set])//["A", "B", "C", "D"]

So our array to weight is very simple to achieve:

var ary = ["A", "B", "C", "D", "C", "a"];
 Let set = new set (ary);
 Let newary = [... set]
 console.log (newary)

5.set can also implement merge collections, cross collections.
For example:
① Merge

var aryone = ["A", "B", "C"];
 var arytwo = ["C", "D", "E"];
 Let Setone = new Set (aryone);
 Let Settwo = new Set (arytwo);
 Let newary = new Set ([... setone,... settwo]);
 Console.log ([... newary])
Second, MAP data structure

1, map in the ES6 is also a data structure, map we have translated here can not be considered a map, but mapping. It's easy to understand that map can map key value pairs to new objects. is a new way of storing data.

How to use 2.map:
The first of ①

  var obj = new Map ();
  Obj.set ("A", 1);
  Obj.get (a)  //1

The second type of ②

var obj = new Map ([[' Name ', ' Jhon '],[' age ',]]);
 Obj.get ("name")//Jhon

When we use the map to process the data, his keys and values appear to be JSON's key-value pairs, but there is a difference, the map's key we can be any data, for example:

  var obj = new Map ();
  Object Let
     ob = {"Name": "ASD"} 
     obj.set (ob, "Objcet")
     Console.log (Obj.get (OB))
 //number
     Obj.set (1, "number")
     Console.log (Obj.get (1))
  //null
     obj.set (NULL, "null")
     Console.log (Obj.get (NULL))
  //Underfind
     Obj.set (Underfind, "Underfind")
     Console.log (Obj.get (Underfind))  

The key points to note are:
① if the key of the map is a simple type of value (number, String, Boolean), then as long as two values are strictly equal, map treats it as a key, including 0 and-0.
② in addition, although Nan is not strictly equal to itself, the map treats it as the same key.

Some of the basic operations of 3.MAP are the same as the set above, and I'm not going to say it again.

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.