Set
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.
The Set itself is a constructor used to generate the Set data structure.
Properties and methods of a Set instance
Instances of the Set structure have the following properties.
Set.prototype.constructor
The: constructor is the defaultSet
Function.Set.prototype.size
Returns theSet
The total number of members of an instance.
The methods of a Set instance fall into two broad categories: operation methods (for manipulating data) and traversal methods (for traversing members). The following four operations are introduced.
Set.prototype.add(value)
: adds a value and returns the Set structure itself.Set.prototype.delete(value)
: Deletes a value and returns a Boolean value indicating whether the deletion was successful.Set.prototype.has(value)
: Returns a Boolean value indicating whether the value isSet
A member of.Set.prototype.clear()
: Clears all members with no return value.
let set = new Set();
set.add("abc");
set.add("abc");
Copy the code
The code above adds members to the Set structure using the add() method, which shows that the Set structure does not add duplicate values.
Traversal operation
An instance of a Set structure has four traversal methods that can be used to traverse members.
Set.prototype.keys()
: returns a traverser for key namesSet.prototype.values()
: returns a traverser for key valuesSet.prototype.entries()
: returns a traverser for key-value pairsSet.prototype.forEach()
: Iterates through each member using the callback function
let set = new Set(['aaa', 'bbb']);
for (let item of set.keys()) {
console.log(item);
}
//aaa
//bbb
Copy the code
Application extension operators for traversal (…) Combined with the Set structure, you can remove duplicate members of an array.
let arr = [1, 2, 2, 3, 5, 5];
let unique = [...new Set(arr)];
// [1, 2, 3,5]
Copy the code
Map
JavaScript objects are essentially collections of key-value pairs (Hash structures), but traditionally strings can only be used as keys. This puts a great limit on its use. The key of a map-like object can be of any type.
Let a = {"name":" zhang SAN "}; const myMap = new Map(); MyMap. Set (a, "death"); console.log(strMapToObj(myMap)); //{'[object object]': 'death penalty'}Copy the code
Instance properties and operation methods
size
Property returns the total number of members of the Map structure.set
Method to set the key namekey
The corresponding key value isvalue
, and return to the entire Map structure. ifkey
If there is already a value, the key value is updated, otherwise the key is generated.get
Methods readkey
Corresponding key value, if can’t findkey
To return toundefined
.has
Method returns a Boolean value indicating whether a key is in the current Map object.delete
Method to delete a key and returntrue
. If the deletion fails, returnfalse
.clear
Method clears all members with no return value.
const myMap = new Map()
.set('yes', true)
.set('no', false);
console.log(strMapToObj(myMap));
// { yes: true, no: false }
Copy the code
Traversal methods
The Map structure natively provides three traverser generating functions and one traversal method.
Map.prototype.keys()
: returns a traverser for key names.Map.prototype.values()
: returns a traverser for key values.Map.prototype.entries()
: returns a traverser for all members.Map.prototype.forEach()
: Traverses all Map members.
Let a = {"name":" zhang SAN "}; const myMap = new Map(); MyMap. Set (a, "death"); for (let key of myMap.keys()) { console.log(key); //{name: 'zhang3'}}Copy the code
The most convenient way to convert a Map to an array is to use the extension operator (…). .
Let a = {"name":" zhang SAN "}; const myMap = new Map(); MyMap. Set (a, "death"); console.log([...myMap]);Copy the code
Map to object If all Map keys are strings, it can be converted to objects losslessly.
function strMapToObj(strMap) {
let obj = {};
for (let [k,v] of strMap) {
obj[k] = v;
}
return obj;
}
const myMap = new Map()
.set('yes', true)
.set('no', false);
strMapToObj(myMap)
// { yes: true, no: false }
Copy the code
If there is a non-string key name, it is converted to a string and used as the object’s key name.