SET data structure

ES6 provides a new data structure, SET. It is similar to an array, but the values of the members are unique and there are no duplicate values.

SET itself is a constructor that generates SET data structures.

Const s = new Set() s.dd (1).add(2).add(1).add(3). / / the Set {1, 2, 3}

Set iterates over objects

If (I =>console.log(I)) for(let I of s) {console.log(I); }
console.log(s.size); //set length console.log(s.hs (100)); //false console.log(s.delete(2)); //true console.log(s); / / {1, 3} the console. The log (s.c Lear ()); // Clear all contents of the collection

Application scenarios

[1,1,1,3,2,1,4,4] const ss = new Set(arr) const narr = Array.from(ss) console.log(ss,narr);  Const result = [] // Convert the array to console.log(result);

MAP data structure

Like objects, they are essentially collections of key-value pairs

The original key object of any type is going to be converted to a string, right

A Map is strictly a collection of key-value pairs that can use keys of any type

The difference between Map and object

To put it simply, the Object type is to add a calculated property, or by adding an external property, it will be converted to a string, and the key of the Object only allows string or symbol, while the map is a good solution to the key type problem

Traditional adding key-value pairs

const obj = {
obj[true] = "value"
obj[11] = "value"
obj[{a:1}] = "value"

console.log(Object.keys(obj)) //[ '11', 'name', 'true', '[object Object]' ]

Map key-value pairs of any type

const m = new Map()
const str = {a:1};
m.set(str,"value") //key , value

Operation method


Map can also take an array as an argument. The members of the array are arrays representing key-value pairs

Const map = new map ([[' name ', 'zhang'], [' title ', 'the Author]]). Map. The map for the size / / 2. From the (' name ') / / true map. Get (' name ') / / "zhang SAN" map. From the (' title ') / / true map. Get (' title ') / / "Author"

So what we actually do up here is

Const items = [[' name ', 'zhang'], [' title ', 'the Author]]. const map = new Map(); items.forEach( ([key, value]) => map.set(key, value) );

Support for the Iterator interface can be used for.. of..