Set and map of JavaScript

Time:2020-12-23

Set

//The set object represents a collection of values, and each value is unique (basic type) (reference types only need different addresses)
//Statement
const s1 = new Set();
console.log(typeof s1,s1);  // object Set {}
//Set method
//Add
s1.add(1);
console.log(s1);   // Set { 1 }
const obj1 = {name:'xuwen'};
const obj2 = {name:'xuwen'};
s1.add(obj1);
S1. Add (ob92); // indicates that the reference type address is different and the value can be the same
console.log(s1);   // Set { 1, { name: 'xuwen' }, { name: 'xuwen' } }
//Delete clear
console.log (S1. Add (2)); // set {1, {Name: 'Xuwen'}, {Name: 'Xuwen'}, 2} returns S1
console.log (S1. Delete (1)); // true returns a Boolean value, and returns true if the deletion is successful
console.log(s1);   // Set { { name: 'xuwen' }, { name: 'xuwen' }, 2 }
s1.clear();
console.log(s1);   // Set {}
//Chahas
s1.add(2);
s1.add('a');
s1.add([1,23]);
console.log(s1);  // Set { 2, 'a', [ 1, 23 ] }
console.log(s1.has(2)); // true
//Iteration
console.log(s1.keys());  // [Set Iterator] { 2, 'a', [ 1, 23 ] }
console.log(s1.values());  // [Set Iterator] { 2, 'a', [ 1, 23 ] }
console.log(s1.entries());  // [Set Entries] { [ 2, 2 ], [ 'a', 'a' ], [ [ 1, 23 ], [ 1, 23 ] ] }
s1.forEach((value,key,s1) => {
    console.log(value,key);
});
// 2 2
// a a
// [ 1, 23 ] [ 1, 23 ]

//Set property
//Size set length
console.log(s1.size);    // 3

//Set instance
//Super typical is array de duplication, there are examples in the array chapter
const arr1 = [1,2,4,5,6,4,5,6];
const newArr1 = [...new Set(arr1)];
console.log(newArr1);  // [ 1, 2, 4, 5, 6 ]
//Array de duplication of mutation instance string
let str1 = "xxwenxxxsdwexx";
let newStr1 = [...new Set(str1)].join('')
console.log(newStr1);   // xwensd
//Set type union intersection difference set
//Set union
const s2 = new Set(['x','w','e']);
const s3 = new Set(['x','w','y']);
const s4 = new Set([...s2,...s3]);
console.log(s4);   // Set { 'x', 'w', 'e', 'y' }
//Set intersection
const s5 = new Set([...s2].filter(item => 
    s3.has(item)
))
console.log(s5);   // Set { 'x', 'w' }
//Set difference set
const s6 = new Set([...s2].filter(item => !s3.has(item)))
console.log(s6);
const s7 = new Set([...s3].filter(item => !s2.has(item)));
const s8 = new Set([...s6,...s7]);
console.log(s8);   // Set { 'e', 'y' }

Map

//Before ES6, the key value in an object can only be a string, and whether it is a numeric type or a reference type, it will be converted to a string
//The map type is introduced in ES6, which can make the key value of any type without conversion
//The map attribute size represents the length of the map
const map = new Map();
console.log(map.size);  // 0
//The method of map
//Increase set
const arr1 = [1,2,4];
const obj1 = {name:'xuwen'};
const num1 = 2020;
const str1 = "xuwen";
const n1 = null;
const u1 = undefined;
map.set(arr1,'x')
    .set(obj1,'u')
    .set(num1,'w')
    .set(str1,'e')
    .set(n1,'n')
    .set(u1,'u');
console.log(map,map.size);
// Map {
//     [ 1, 2, 4 ] => 'x',
//     { name: 'xuwen' } => 'u',
//     2020 => 'w',
//     'xuwen' => 'e',
//     null => 'n',
//     undefined => 'u'
//   } 6

//Delete clear
console.log(map.delete(arr1));  // true
console.log(map);
//Clear will clear all the keys and values, so I won't show you
//Have get
console.log(map.has(u1));   // true
console.log ( map.get (num1)); // W get is the value corresponding to the returned key
let year = 2007;
console.log ( map.get (year)); // undefined
//Iteration
console.log(map.keys());  // [Map Iterator] { { name: 'xuwen' }, 2020, 'xuwen', null, undefined }
console.log(map.values());  // [Map Iterator] { 'u', 'w', 'e', 'n', 'u' }
console.log(map.entries());
// [Map Entries] {
//     [ { name: 'xuwen' }, 'u' ],
//     [ 2020, 'w' ],
//     [ 'xuwen', 'e' ],
//     [ null, 'n' ],
//     [ undefined, 'u' ]
//   }