ES6 set and map data structure

Time:2020-11-26

Set is a new data structure of ES6, which is characterized by the absence of members with the same value

Set

Set itself is a constructor used to generate the set data structure

const set = new Set();

[1,2,2,3,4,5,6,7,5,8,9].forEach((x)=>{
    set.add(x);
})

console.log(set);  // [1,2,3,4,5,6,7,8,9]

The set constructor takes an array or class array as a parameter to complete initialization

const set = new Set([1,2,2,3,4,5,6,7,5,8,9])

console.log(set); // [1,2,3,4,5,6,7,8,9]

The set instance object has the size attribute, which is equal to the length in the array

Using set data structure to complete array de duplication

const newArr = […new Set([1,2,2,3,4,5,5])]

console.log(newArr); // [1,2,3,4,5]

Using set data structure to complete string de duplication

const newStr = […new Set(“ababcc”)].join(”);

console.log(newStr); //”abc”

Set uses a similar = = = method to detect whether each member is equal, butNo type conversion will occurFor example, 5 and “5”, set determines that they are two different values. If they are two Nan, they are equal values. Then set will save only one of them. The exception is that two empty objects {} are treated as different values

ES6 set and map data structure

An instance of a set structure has the following properties

Set.prototype.constructor Construction method, the default is set function

Set.prototype.size Returns the total number of set instance members

Four methods of operating data of set

Set.prototype.add (value) / / add a value and return the set structure itself, which can be operated in chain mode

Set.prototype.delete (value) / / deleting a value returns a Boolean value indicating whether the deletion was successful

Set.prototype.has (value) / / returns a Boolean value indicating whether the value is a member of a set

Set.prototype.clear () / / clear all values of set instance, no return value

The difference between object and set to determine whether a key is included

//Object

const properties = {

   width:”1″,

   height:”1″,

}

if(properties[“width”]){
    console.log (“existing width”);
}

// Set

const properties = new Set();

properties.add(“width”).add(“height”);

if(properties.has(“width”)){

console.log (“existing width”);

}

use Array.from () converts the set data structure to an array

const items = new Set([1,2,3,4,5]);

const array = Array.from(items);

console.log(array); // [1,2,3,4,5]

use Array.from () and complete the array

function dedupe(array){

 return Array.from(new Set(array));

}

const res = dedupe([1,2,2,3,4,5]);

console.log(res); //[1,2,3,4,5];

Four kinds of traversers of set structure instance

Set.prototype.key () / / returns the traverser of the key name

Set.prototype.value () / / returns the traverser of the key value

Set.prototype.entries () / / returns the traverser of key value pair

Set.prototype.forEach () / / use the callback function to traverse each member

The traversal order of set is the insertion order

Set has no key name, only key value (key name is key value)

You can omit the keys() or values() methods and directly use for… Of to traverse the set

Consistent with the foreach of array, foreach accepts two parameters. The first is the callback function. The parameters are key value, key name and structure itself. The second parameter is the this point of callback function binding

The extension operator (…) is used on the set structure, and the internal mechanism is for… Of. Therefore, we can use the expansion operator to implement array de duplication operation, and indirectly implement the map and filter traversal of the set structure

//Array de duplication

[…new Set([1,2,2,3,4])]  // [1,2,3,4]

const set = new Set([1,2,3,4]);

//Map traversal of set

const res = new Set([…set].map(v=>v*2)); 

console.log(res) //Set [2,4,6,8]

//Filter traversal of set

const res2 = new Set([…set].filter(v=>v>2));

console.log(res) //Set [3,4]

Using set to realize Union, intersection and difference

//Union Union

let a = new Set([1,2,3,4]);

let b = new Set([3,4,5,6]);

const res = new Set([…a,…b]);

console.log(res) //Set [1,2,3,4,5,6]

//Intersect intersection

const intersect = new Set([…a].filter(v=>b.has(v)));

console.log(intersect) //Set [3,4]

//Difference difference set

const Difference = new Set([…a].filter(v=>!b.has(v)));

//The difference set of a relative to B

console.log(Difference); //Set [1,2]

Change the internal members of set structure

At present, you cannot change the set structure directly, but you can use set mapping or Array.from () indirect implementation

const set = new Set([1,2,3,4]);

set = new Set([…set].map(v=>v*2)); 

console.log(set) // Set [2,4,6,8];

set = new Set(Array.from(set,val=>val*2))

console.log(set) //Set [4,8,12,16];

Map