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.constructorThe: constructor is the defaultSetFunction.
  • Set.prototype.sizeReturns theSetThe 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 isSetA 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 names
  • Set.prototype.values(): returns a traverser for key values
  • Set.prototype.entries(): returns a traverser for key-value pairs
  • Set.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

  • sizeProperty returns the total number of members of the Map structure.
  • setMethod to set the key namekeyThe corresponding key value isvalue, and return to the entire Map structure. ifkeyIf there is already a value, the key value is updated, otherwise the key is generated.
  • getMethods readkeyCorresponding key value, if can’t findkeyTo return toundefined.
  • hasMethod returns a Boolean value indicating whether a key is in the current Map object.
  • deleteMethod to delete a key and returntrue. If the deletion fails, returnfalse.
  • clearMethod 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.