JavaScriptのセットとMap


セット
// set        ,          (    )(             )
//   
const s1 = new Set();
console.log(typeof s1,s1);  // object Set {}
// set  
//   add
s1.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 clear
console.log(s1.add(2));   // Set { 1, { name: 'xuwen' }, { name: 'xuwen' }, 2 }   s1
console.log(s1.delete(1));  // true               true
console.log(s1);   // Set { { name: 'xuwen' }, { name: 'xuwen' }, 2 }
s1.clear();
console.log(s1);   // Set {}
//   has
s1.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   
//   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      key          
//   has  get
console.log(map.has(u1));   // true
console.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' ]
//   }