zoukankan      html  css  js  c++  java
  • es6(10)--Set,Map(1)

     1 //Set
     2 {
     3     let list=new Set();
     4     list.add(5);//添加
     5     list.add(7);
     6     //属性size就是长度
     7     console.log('size',list.size);//2
     8 }
     9 {
    10     let arr = [1,2,3,4,5];
    11     let list = new Set(arr);
    12     console.log(list.size);//5
    13 }
    14 {
    15     //去重
    16     let list = new Set();
    17     list.add(1);
    18     list.add(2);
    19     list.add(1);
    20     console.log('size',list);//1,2,没有最后的1
    21 
    22     let arr=[1,2,3,1,2];
    23     let list2=new Set(arr);
    24     console.log('unique',list2);//1,2,3,没有后面的1,2
    25 }
    26 {
    27     let arr=['add','delete','clear','has'];
    28     let list=new Set(arr);
    29     console.log('has',list.has('add'));//查看是否存在
    30     console.log('delete',list.delete('add'),list);//删除
    31     console.log('clear',list.clear('add'),list);//清空
    32 }
    33 //遍历
    34 {
    35     let arr=['add','delete','clear','has'];
    36     let list=new Set(arr);
    37 
    38     for(let key of list.keys()){
    39         console.log('key',key);
    40     }
    41     for(let value of list.values()){
    42         console.log('keys',value);
    43     }
    44     for(let [key,value] of list.entries()){
    45         console.log('entries',key,value);
    46     }
    47     list.forEach(function(item){
    48         console.log(item)
    49     })
    50 }
    51 //WeakSet:元素只能是对象,不能是数值,不能遍历,没有clear方法,没有set属性
    52 {
    53     let weakList=new WeakSet();
    54     let arg={};
    55     weakList.add(arg);
    56     //weakList.add(2);不能是数值
    57     console.log('weakList',weakList);
    58 }
    59 //Map,遍历和set一样
    60 {
    61     let map= new Map();
    62     let arr=['123'];
    63     map.set(arr,456);//添加元素(key,value)
    64     console.log('map',map,map.get(arr));//get(key)获取值
    65 }
    66 {
    67     let map= new Map([['a',123],['b',456]])
    68     console.log(map,map.size)
    69     console.log(map.delete('a'),map)
    70     console.log(map.clear(),map)
    71 }
    72 //同WeakSet
    73 {
    74     let weakmap=new WeakMap();
    75 }
  • 相关阅读:
    P4127 [AHOI2009]同类分布
    区间DP
    P3146 [USACO16OPEN]248
    P1241 括号序列
    P2858 [USACO06FEB]奶牛零食Treats for the Cows
    P2602 [ZJOI2010]数字计数&P1239 计数器&P4999 烦人的数学作业
    数位DP
    jquery生成元素注册事件无效,及事件委托的使用
    Jquery ajax运用执行顺序有误怎么解决
    html页面输入框input的美化
  • 原文地址:https://www.cnblogs.com/chenlw/p/9227888.html
Copyright © 2011-2022 走看看