Set

Is essentially a constructor that creates a deduplicate array.

The method of the Set

  1. Set.prototype.add(value) : Adds a value and returns the Set structure itself.
  2. Set.prototype.delete(value) : Deletes a value and returns a Boolean value indicating whether the deletion was successful.
  3. Set.prototype.has(value) : Returns a Boolean value indicating whether the value is a member of Set.
  4. Set.prototype.clear() : Clears all members with no return value.
  5. Set.prototype.keys() : iterator that returns key names.
  6. Set.prototype.values() : iterator that returns key values.
  7. Set.prototype.entries() : Returns a traverser for key and value pairs.
  8. Set.prototype.foreach () : Use the callback function to iterate over each member.

Note that set has no key name, so the key value is the same value.

1

let arr = [3.5.2.2.5.5];
let arrString = 'ausiejsaasiduwq';
let arrSet = new Set(arr);
let strSet = new Set(arrString);
console.log(arrSet); // Set(3) { 3, 5, 2 }
console.log(strSet); // Set(9) { 'a', 'u', 's', 'i', 'e', 'j', 'd', 'w', 'q' }
arrSet.add(4);
arrSet.delete(3);
arrSet.has(5);
strSet.clear();
console.log(arrSet); // Set(3) { 5, 2, 4 }
console.log(strSet); // Set(0) {}
Copy the code

2. Set with extension operator (…) The use of the

let arr = [3.5.2.2.5.5];
let arrString = 'ausiejsaasiduwq';
let arrSet = [...new Set(arr)];
let strSet = [...new Set(arrString)];
console.log(arrSet); // [3, 5, 2]
console.log(strSet); 
//['a', 'u', 's','i', 'e', 'j','d', 'w', 'q']
Copy the code

3. Remove the reorder

Bubble sort

let arr = [3.5.2.2.5.5];
let arrString = 'ausiejsaasiduwq';
let arrSet = [...new Set(arr)];
let strSet = [...new Set(arrString)];
let arrSort = arrSet.sort((x,y) = >{
	console.log(x,y)
	    if (x < y) {
	        return -1;
	    }
	    if (x > y) {
	        return 1;
	    }
	    return 0;
});
let strSort = strSet.sort((x,y) = >{
// Strings are treated uniformly in ASCII size comparisons
		str1 = x.toUpperCase(); 
		str2 = y.toUpperCase();
	    if (x < y) {
	        return -1;
	    }
	    if (x > y) {
	        return 1;
	    }
	    return 0;
});
console.log(arrSort); // [2, 3, 5]
console.log(strSort); // ['a', 'd', 'e','i', 'j', 'q','s', 'u', 'w']
Copy the code

Map

Is essentially a collection of key-value pairs, but traditionally only strings can be used as keys.

The method of the Map

  1. Map.prototype.set(key, value) : Sets the corresponding key to value and returns the entire Map structure. If the key already has a value, the key value is updated.
  2. Map.prototype.get(key) : reads the corresponding key. If the key is not found, return undefined.
  3. Map.prototype.has(key) : Returns a Boolean value indicating whether a key is in the current Map object.
  4. Map.prototype.delete(key) : deletes a key and returns true. If deletion fails, return false.
  5. Map.prototype.clear() : Clears all members.
  6. Map.prototype.keys() : iterator that returns key names.
  7. Map.prototype.values() : iterator that returns key values.
  8. Map.prototype.entries() : Returns a traverser for key and value pairs.
  9. Map.prototype.foreach () : Use the callback function to iterate over each member.

Basic usage of MAP

let obj = {'str1':1.'str2':2};
let map = new Map(Object.entries(obj));
map.set('str3'.3)
console.log(Object.entries(obj)); // [ [ 'str1', 1 ], [ 'str2', 2 ] ]
console.log(map); // Map(3) { 'str1' => 1, 'str2' => 2, 'str3' => 3 }
console.log(map.entries()); 
// [Map Entries] { [ 'str1', 1 ], [ 'str2', 2 ], [ 'str3', 3 ] }
Copy the code