码迷,mamicode.com
首页 > 其他好文 > 详细

es6 语法 (set 和 map)

时间:2017-10-20 21:33:05      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:new   清空   cti   lis   key   color   语法   weak   col   

{
    let list = new Set();
    list.add(5);
    list.add(7);

    console.log(‘size‘, list, list.size); //{5, 7} 2
}

{
    let arr = [1, 2, 3, 4, 5];
    let list = new Set(arr);

    console.log(‘size‘, list.size); //5
}

{
    let list = new Set();
    list.add(1);
    list.add(2);
    list.add(1);

    console.log(‘list‘, list); //{1,2}

    let arr = [1, 2, 3, 1, ‘2‘];
    let list2 = new Set(arr);

    console.log(‘unique‘, list2); //{1,2,3,‘2‘}
}

{
    let arr = [‘add‘, ‘delete‘, ‘clear‘, ‘has‘];
    let list = new Set(arr);

    console.log(‘has‘, list.has(‘add‘)); //true
    console.log(‘delete‘, list.delete(‘add‘), list); //{"delete", "clear", "has"}
    list.clear(); //清空
    console.log(‘list‘, list); //{}
}

{
    let arr = [‘add‘, ‘delete‘, ‘clear‘, ‘has‘];
    let list = new Set(arr);
    console.log(‘list‘, list); //{"add":"add","delete":"delete","clear":"clear","has":"has"}
    for (let key of list.keys()) {
        console.log(‘keys‘, key); //add delete clear has
    }
    for (let value of list.values()) {
        console.log(‘value‘, value); //add delete clear has
    }
    for (let [key, value] of list.entries()) {
        console.log(‘entries‘, key, value); // add add;delete delete;clear clear;has has;
    }

    list.forEach(function(item) { console.log(‘item‘, item); }) //add delete clear has
}


{
    let weakList = new WeakSet();

    let arg = {};

    weakList.add(arg);

    // weakList.add(2);

    console.log(‘weakList‘, weakList); //{}
}

{
    let map = new Map();
    console.log(‘mapfirst‘, map); //{}
    let arr = [‘123‘];

    map.set(arr, 456);
    console.log(‘maptwo‘, map);//{array(1)=>456}
    console.log(‘map‘, map, map.get(arr)); //456
}

{
    let map = new Map([
        [‘a‘, 123],
        [‘b‘, 456]
    ]);
    console.log(‘map args‘, map);//{"a"=>123,"b"=>456}
    console.log(‘size‘, map.size);//2
    console.log(‘delete‘, map.delete(‘a‘), map);//true , {"b"=>456}
    console.log(‘clear‘, map.clear(), map);//{}
}

{
    let weakmap = new WeakMap();

    let o = {};
    weakmap.set(o, 123);
    console.log(weakmap.get(o));//123
}

 

es6 语法 (set 和 map)

标签:new   清空   cti   lis   key   color   语法   weak   col   

原文地址:http://www.cnblogs.com/Byme/p/7701183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!