set,map

  1. set

    . 1  {
     2      the let List = new new the Set ();
     . 3      List.add (. 5); // SET additive elements with the Add 
    . 4      List.add (. 7 );
     . 5      the console.log ( 'size', list.size)   // Get length 2 
    . 6  }
     7  {
     8      // can be set directly to the initialization 
    . 9      the let ARR = [1,2,3,4,5 ];
     10      the let List = new new the set (ARR);
     . 11      the console.log ( 'size', List .size)   // . 5 
    12 is }

    The value set is the only application: an array of de-duplication

    . 1  {
     2      the let List = new new the Set ();
     . 3      List.add (. 1 );
     . 4      List.add (2 );
     . 5      List.add (. 1 );   
     . 6      the console.log ( 'List' , List)  
     . 7  
    . 8      the let arr2 is = [1,2,3,2,1];   // string is not automatically converted, and 2 '2' is not the same value, not to the weight 
    . 9      the let List2 = new new the Set (arr2 is);
     10      Console. log ( 'List2', List2)     // l, 2,3 
    . 11 }

    Add, remove, remove, contain

    1 {
    2     let arr = ['add','delete','clear','has'];
    3     let list = new Set(arr);
    4     console.log('has',list.has('add'))
    5     console.log('delete',list.delete('add'),list)
    6     list.clear();
    7     console.log(list)
    8 }

    Traversal

     1 {
     2     let arr = ['add','delete','clear','has'];
     3     let list = new Set(arr);
     4      for(let key of list.keys()){
     5          console.log('key',key)
     6      }
     7      for(let value of list.values()){
     8          console.log('value',value)
     9      }
    10      for(let [key,value] of list.entries()){
    11          console.log('entries',key,value)
    12      }
    13 
    14      list.forEach(item=>{console.log(item)})
    15 }
  2. map

    . 1  {
     2      // Map 
    . 3      the let Map = new new the Map ();
     . 4      the let ARR = [ '123' ];
     . 5  
    . 6      map.set (ARR, 456); // Map SET additive elements, to key, value added, key can be any type 
    . 7      the console.log (Map, as map.get (ARR))   // Get GET 
    . 8  }
     . 9  
    10  {
     . 11      the let Map = new new the Map ([[ 'A',. 1], [ 'B', 2 ]])    // Note format 
    12 is      the console.log (Map)
     13 is      the console.log ( 'size' , map.size)
     14      the console.log ( 'Delete', Map. Delete('a'),map)
    15     console.log('clear',map.clear(),map)
    16 
    17 }

     

  3. WeakSet,WeakMap

    . 1  {
     2      // weakSet only data objects is weak reference is not detected elsewhere not used 
    3      // no clear method can not traverse, there is no size attribute 
    . 4      the let weakList = new new WeakSet ();
     . 5      the let Arg = {};
     . 6      weakList.add (Arg);
     . 7      the console.log (weakList)
     . 8  }
     . 9  {
     10      // Key values received only WeakMap object no Clear, can not traverse 
    . 11      the let WeakMapList = new new WeakMap ();
     12 is      the let O = {};
     13 is      WeakMapList.set (O, 123 );
     14      the console.log (WeakMapList)
     15 }

     

  4. Comparative map, set and array of  

     Array comparative map

    1  {
     2      // horizontal comparison data structures, CRUD 
    . 3      the let Map = new new the Map ();
     . 4      the let Array = [];
     . 5      // increase 
    . 6      map.set ( 'T', 1 );
     . 7      Array.push ( T {:. 1 });
     . 8      console.info ( 'increase' , Map, Array)
     . 9  
    10      // search 
    . 11      the let map_exist = map.has ( 'T');   // to true 
    12 is      the let array_exist = array.find (Item => item.t); // returns the current value 
    13 is      console.info ( 'check' , map_exist, array_exist)
     14  
    15     // change 
    16      map.set ( 'T', 2 );
     . 17      Array.ForEach (Item => item.t item.t = 2:? '');   // determines if the value is present, modification, there is no without modification 
    18 is      console.info ( 'change' , Map, Array)
     . 19  
    20 is      // delete 
    21 is      Map. Delete ( 'T' )
     22 is      the let index = array.findIndex (Item => item.t); // Find element the subscript location 
    23 is      Array.splice (index,. 1);   // delete the element at the location 
    24      console.info ( 'delete' , Map, Array)
     25  
    26 is }

    Comparative set and Array

     1 {
     2     let set = new Set();
     3     let array= []; 
     4     //
     5     set.add({t:1});
     6     array.push({t:1});
     7     console.info('add',set,array)
     8 
     9     //
    10     let setExist = set.has({t:1});
    11     let arrayExist = array.find(item=>item.t)
    12     console.info('get',setExist,arrayExist)
    13 
    14     //
    15     set.forEach(item=>item.t?item.t = 3 : '');
    16     array.forEach(item=>item.t?item.t = 3 :'');
    17     console.info('update',set,array)
    18 
    19     //
    20     set.forEach(item=>item.t?set.delete(item.t):'')
    21     let index = array.findIndex(item => item.t);
    22     array.splice(index,1);
    23     console.info('delete',set,array)
    24 }
  5. Comparative map, set with object of

     1 {
     2     //map set object 对比
     3     let item = {t:1};
     4     let map = new Map();
     5     let set = new Set();
     6     let obj = {};
     7 
     8     //
     9     map.set('t',1);
    10     set.add(item);
    11     obj['t'] = 1;
    12 
    13     console.info('add',map,set,obj)
    14 
    15     //
    16     console.table({
    17         map_exist:map.has('t'),
    18         set_exist:set.has(item),
    19         obj_exist:'t' in obj      //对象用in查找
    20     })
    21 
    22     //
    23     map.set('t',2);
    24     item.t=2;
    25     obj['t']=2;
    26     console.log('update',map,set,obj)
    27 
    28     //
    29     map.delete('t');
    30     set.delete(item);
    31     delete obj['t'];
    32     console.info('delete',map,set,obj)
    33 }

     

Guess you like

Origin www.cnblogs.com/zynkl1314/p/12023525.html