Set

A Set object allows you to store a unique value of any type, either the original value or the object reference essence: constructor, used to generate a Set data structure

describe

A Set object is a collection of values, and you can iterate over its elements in the order in which they were inserted. The elements in a Set occur only once, that is, the Set element is unique. Equivalent to a set, you can do the union intersection operation.

The value of the equal

For primitive data types (Boolean, number, String, NULL, undefined), only one value is stored if the same value is stored, and only one reference address is stored for reference types.

  • +0 and -0 are identical in the sense that they store uniqueness, so they cannot be repeated.
  • Undefined and undefined are identical, so they cannot be repeated.
  • NaN is not identical to NaN, but only one cannot be repeated in a Set.

Attribute method

  • Size property: Returns the number of collection elements (similar to the length of an array)
  • Add (value) method: Wants to add an element to the collection. If the added element already exists, no action is taken.
  • Delete (value) method: Deletes the element value from the collection
  • Has (value) method: checks whether value is in the set and returns true or false
  • The clear() method: clears the collection
  • ForEach () method: Executes the provided callbacks in order of insertion of the elements in the collection

Application scenario array de-duplication, intersection, union, difference and so on

// Array decrement. newSet([1.1.2.2.3])

/ / and set
let arr1 = [1.2.3]
let arr2 = [2.3.4]
let newArr = [...new Set([...arr1, ...arr2])]
/ / intersection
let arr1 = [1.2.3]
let arr2 = [2.3.4]
let set1 = new Set(arr1)
let set2 = new Set(arr2)
let newArr = []
set1.forEach(item= > {
    set2.has(item) ? newArr.push(item) : ' '
})
console.log(newArr)
/ / difference set
let arr1 = [1.2.3]
let arr2 = [2.3.4]
let set1 = new Set(arr1)
let set2 = new Set(arr2)
let newArr = []
set1.forEach(item= > {
    set2.has(item) ? ' ' : newArr.push(item)
})
set2.forEach(item= > {
    set1.has(item) ? ' ' : newArr.push(item)
})
console.log(newArr)
Copy the code

Map

Map objects hold key-value pairs and can remember the original insertion order of the keys. Any value (object or original value) can be left or right about a key or a value

Description:

A Map object iterates based on the insertion order of the elements in the object, and a for of loop returns an array of the form [key,value] after each iteration

The key of equal

For primitive data types (Boolean, number, String, NULL, undefined), only one value is stored if the same value is stored, and only one reference address is stored for reference types.

  • +0 and -0 are identical in the sense that they store uniqueness, so they cannot be repeated.
  • Undefined and undefined are identical, so they cannot be repeated.
  • NaN and NaN are not identical, but only one cannot be repeated.

The difference between Map and Object

Map Object
Additional keys Map contains no keys by default, only inserted keys An Object has a prototype. The key name on the prototype chain may conflict with the key name you set on the Object. ES5 can be used for this purposeObject.create(null)Create an object without a prototype
The type of the key A Map’s keys can be of any value, including functions, objects, or any primitive type The key of an Object must be a String or Symbol
The order of the keys Keys in a Map are ordered, so that when iterated, a Map object returns key values in the order they were inserted The key of an Object is unordered
Size Obtained by the size attribute The key value of Objec can only be calculated manually
The iteration Iterable, can be iterated directly You need to get the key somehow in order to be iterated over
performance This is better when key pairs are frequently added or deleted Not optimized

Attribute method

  • Size property: returns the length of the dictionary (similar to the length of an array)
  • Values () method: Returns an iterable containing the value of each element inserted in the Map in order
  • Set (key,value) method: adds a new element to the dictionary
  • Get (key) method: finds a specific value by key and returns it
  • Has (key) method: check whether the key exists in the dictionary
  • Delete (key) method: removes the corresponding data from the dictionary by key
  • The clear() method: Empty the dictionary
  • ForEach () method: Executes the provided callbacks in order of insertion of the elements in the collection

traverse

var myMap = new Map(a); myMap.set("0"."foo");
myMap.set(1."bar");
myMap.set({}, "baz");

for (const [key, value] of myMap) {
  console.log(key, value);
}

myMap.forEach((value, key) = > {
  console.log(value, key);
});
Copy the code

conversion

var arr = [
  [1.2],
  [3.4],
  [5.6]];var map = new Map(arr);
console.log(map); //Map { 1 => 2, 3 => 4, 5 => 6 }
console.log(Array.from(map)); //[[1, 2], [3, 4], [5, 6]]
Copy the code

Copy the let mapV = newMap (map)

WeakSet

A WeakSet object allows weak reference objects to be stored in a collection

WeakSet and Set differences:

  • WeakSet can only store object references, not values, while Set objects can
  • The object values stored in the WeakSet object are weakly referenced, that is, the garbage collection mechanism does not consider the WeakSet reference to the object. If there is no other variable or attribute reference to the object value, the object will be garbage collected. (Not considering that the object still exists in the WeakSet), so how many member elements there are in the WeakSet object depends on whether the garbage collection mechanism is running. The number of members before and after the operation may be inconsistent. After the end of traversal, some members may not be available and are garbage collected. Therefore, ES6 provides that the WeakSet object cannot be traversed, and there is no way to get all the elements it contains.

Attribute: constructor:

  • Add (value) method: Add an element to WeakSet. If the added element already exists, no action is taken.
  • The delete(value) method deletes the element value
  • Has (value) method: Judge whether value is included in the WeakSet object
  • The clear() method clears all elements

WeakMap

A WeakMap object is a set of key values where the keys are weak references. Note: The key must be a weak reference and the value can be arbitrary. Note: WeakMap weakly references only the key name, not the key value. Key values are still normal references.

Difference between WeakMap and Map: In WeakMap, each key’s reference to its own reference object is weak reference. When there is no other reference and the key references the same object, the object will be garbage collected, and the corresponding key becomes invalid. Therefore, the key of WeakSet is not enumerable. Attribute: constructor:

  • Set (key,value) method: Sets a group of key-associated objects
  • Delete (key) method: removes the object associated with the key
  • Has (value) method: Judge whether value is included in the WeakSet object
  • Get (key) : returns the object associated with the key, or undefined

conclusion

  • Set
    • The member is unique, unordered, and does not duplicate
    • Similar to array collections, key values and key names are consistent (only key values. No key name)
    • You can iterate through it with methods like add, delete, has
  • WeakSet
    • Only references can be stored, not values
    • Members are weak references and are collected by the garbage collection mechanism
    • Can’t iterate, methods add, delete, has
  • Map
    • The key name is unique and not repeatable
    • Just like a set, a set of key-value pairs, any value can be a key or a value
    • Can traverse, can convert various data formats, methods get, set, HAS, delet
  • WeakMap
    • Only objects are accepted as key names. Values of other types are not accepted as key names. Key values can be arbitrary
    • The key name is a drag reference, and the object that the key name points to is collected by the garbage collection mechanism
    • Cannot traverse, methods get, set, has, delete