Set

// set对象示意值得汇合,且每个值得都是惟一的(根本类型)(援用类型只有地址不一样即可)// 申明const s1 = new Set();console.log(typeof s1,s1);  // object Set {}// set办法// 增 adds1.add(1);console.log(s1);   // Set { 1 }const obj1 = {name:'xuwen'};const obj2 = {name:'xuwen'};s1.add(obj1);s1.add(obj2);    //  阐明援用类型  地址不一样  值是能够一样的console.log(s1);   // Set { 1, { name: 'xuwen' }, { name: 'xuwen' } }// 删  delete clearconsole.log(s1.add(2));   // Set { 1, { name: 'xuwen' }, { name: 'xuwen' }, 2 } 返回s1console.log(s1.delete(1));  // true  返回布尔值  删除胜利返回trueconsole.log(s1);   // Set { { name: 'xuwen' }, { name: 'xuwen' }, 2 }s1.clear();console.log(s1);   // Set {}// 查 hass1.add(2);s1.add('a');s1.add([1,23]);console.log(s1);  // Set { 2, 'a', [ 1, 23 ] }console.log(s1.has(2)); // true// 迭代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属性// size set长度console.log(s1.size);    // 3// set实例// 超级典型的就是数组去重   在数组章节有过实例const arr1 = [1,2,4,5,6,4,5,6];const newArr1 = [...new Set(arr1)];console.log(newArr1);  // [ 1, 2, 4, 5, 6 ]// 数组去重的变异实例   字符串去重let str1 = "xxwenxxxsdwexx";let newStr1 = [...new Set(str1)].join('')console.log(newStr1);   // xwensd// 汇合类型  并集   交加   差集// set并集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 交加const s5 = new Set([...s2].filter(item =>     s3.has(item)))console.log(s5);   // Set { 'x', 'w' }// 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

// 在ES6之前   对象中的key值只能是字符串   不论是数值类型还是援用类型都会转换成字符串// 在es6中  引入了map类型   他能够让key值为任何类型而不必去转换// map属性  size   示意map的长度const map = new Map();console.log(map.size);  // 0// map的办法// 增 setconst 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 clearconsole.log(map.delete(arr1));  // trueconsole.log(map);// clear会革除所有的key和值   就不演示了// 查 has  getconsole.log(map.has(u1));   // trueconsole.log(map.get(num1));  // w  get是返回key对应的值let year = 2007;console.log(map.get(year));    // undefined  如果没有就返回undefined// 迭代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' ]//   }