Set

ES6 provides a Set data container, which is an ordered list of values that can be stored without duplicates.

Basic usage

We can create a Set with new Set(), and then add items to the Set with the add method:

const s = new Set(a); [2.3.5.4.5.2.2.'5'].forEach(x= > s.add(x));

for (let i of s) {
  console.log(i);
}
// 2 3 5 4 '5'     
Copy the code

The characteristics of

  • The values of members are unique; there are no duplicate values.

  • When you add a value to a Set, no type conversion occurs, so 5 and “5” are two different values.

Operation method

  • Add () adds data items to the Set

  • Has () checks whether a value exists in the Set

  • Delete () deletes a value from a Set

  • Clear () removes all values from the Set

let set = new Set([1.2.3.3.3.3]);
set.add(4); // 1, 2, 3, 4
console.log(set)
console.log(set.size);/ / 4
console.log(set.has(5)); //false

set.delete(1);

console.log(set.has(1)); //false
console.log(set.size); / / 3

set.clear();
console.log(set.size); / / 0
Copy the code

Traversal methods

An instance of a Set structure has four traversal methods that can be used to traverse members.

  • Keys () : returns a traverser for key names

  • Values () : Iterator that returns key values

  • Entries () : Returns a traverser for key and value pairs

  • ForEach () : Iterates through each member using the callback function

let set = new Set(['red'.'green'.'blue']);

for (let item of set.keys()) {
  console.log(item);
}
// red
// green
// blue

for (let item of set.values()) {
  console.log(item);
}
// red
// green
// blue

for (let item of set.entries()) {
  console.log(item);
}
// ["red", "red"]
// ["green", "green"]
// ["blue", "blue"]

let set2 = new Set([1.2.3]);
set2.forEach((value, key) = > console.log(value * 2))/ / 2
/ / 4
/ / 6
Copy the code

Set object

Array to heavy

let arr = [3.5.2.2.5.5];
let unique = [...new Set(arr)];
/ / [3, 5, 2]
Copy the code

Union

var a = new Set([1.2.3]);
var b = new Set([4.3.2]);
var union = new Set([...a, ...b]); // {1, 2, 3, 4}
Copy the code

Intersect

var a = new Set([1.2.3]);
var b = new Set([4.3.2]);
var intersect = new Set([...a].filter(x= > b.has(x))); / / {2, 3}
Copy the code

Difference set

var a = new Set([1.2.3]);
var b = new Set([4.3.2]);
var difference = new Set([...a].filter(x= >! b.has(x)));/ / {1}
Copy the code

Map

In ES6, Map data structures are provided to store key-value pairs, where keys are de-duplicated by comparison with object.is (). The data type of keys can be either basic or Object, and the value can be any type of data.

Basic usage

You can create a Map using new Map(), and then add data items to the Map using the set() method:

 let map = new Map(a); map.set('title'.'hello world');
 map.set('year'.'2018');
 
 console.log(map.size); / / 2
Copy the code

The Map is initialized

// Use arrays to create maps
let map = new Map([['title'.'hello world'], ['year'.'2018']]);
console.log(map.has('title')); //true
console.log(map.has('year')); //true
console.log(map.size); / / 2
Copy the code

Operation method

  • Set () : Adds key-value pairs to the Map

  • Get () : extracts the value from the Map

  • Has (): Checks whether a data item exists in the Map

  • Delete (): Deletes a data item from the Map

  • Clear (): deletes all data items in the Map

 let map = new Map(a); map.set('title'.'hello world');
 map.set('year'.'2018');
 
 console.log(map.get('title')); // hello world
 console.log(map.has('year')); //true
 map.delete('title');
 console.log(map.has('title')); //false
 map.clear();
 console.log(map.size); / / 0
Copy the code

Traversal methods

Map natively provides three traverser generating functions and one traversal method.

  • Keys () : returns a traverser for key names.

  • Values () : Iterator that returns key values.

  • Entries () : Returns a traverser for all members.

  • ForEach () : traverses all Map members.

Note in particular that the Map traversal order is the insertion order.

let map = new Map([['F'.'no'],
  ['T'.'yes']]);for (let key of map.keys()) {
  console.log(key);
}
// "F"
// "T"

for (let value of map.values()) {
  console.log(value);
}
// "no"
// "yes"

for (let item of map.entries()) {
  console.log(item[0], item[1]);
}
// "F" "no"
// "T" "yes"

/ / or
for (let [key, value] of map.entries()) {
  console.log(key, value);
}

// Equivalent to using map.entries()
for (let [key, value] of map) {
  console.log(key, value);
}

 map.forEach((value,key,ownerMap) = >{
      console.log(key, value);
 });
 // "F" "no"
// "T" "yes"
Copy the code

Extended operators (…)

A faster way to convert a Map structure into an array structure is to use the extension operator (…). .

let map = new Map([[1.'one'],
  [2.'two'],
  [3.'three']]); [...map.keys()]/ / [1, 2, 3]

[...map.values()]
// ['one', 'two', 'three']

[...map.entries()]
// [[1,'one'], [2, 'two'], [3, 'three']]

[...map]
// [[1,'one'], [2, 'two'], [3, 'three']]
Copy the code

Map traversal and filtering

Map traversal and filter can be realized by combining the map and filter methods of arrays. (Map does not have 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) ); / / generated Map structure {1 = > 'a', 2 = > 'b'} let map2 = new Map (map0 [...]. The Map (((k, v)) = > [k * 2, '_' + v])); {2 => '_A ', 4 =>' _B ', 6 => '_c'}Copy the code