ES-6--数据结构

Set的用法,WeakSet的用法,Map的用法,WeakMap的用法

Set()基本用法

{
    let list=new Set();
    list.add(5);
    list.add(7);
    console.log(list.size)
    console.log(list)
    //2
    //Set(2) {5, 7}
}
{
    let arr=[1,2,3,4,5];
    let list=new Set(arr);
    console.log(list)
    //Set(5) {1, 2, 3, 4, 5}
}
{
    let list = new Set();
    list.add(1);
    list.add(2);
    list.add(1);
    console.log(list)
    //Set(2) {1, 2}
}
{
    //Set数组去重
    let arr=[1,2,3,4,4,6,7,1];
    let list=new Set(arr);
    console.log(list)
    //不会做数据类型的转换数字2和字符串2不一样
    //{1, 2, 3, 4, 6, 7}
}

Set的add,delete,has,clear方法

{
    let arr=["add","delete","clear","has"];
    let list=new Set(arr);
    console.log(list.has('add'));
    //true
    console.log(list.delete('add'));
    //true
    list.clear()
    console.log(list);
    //Set(0) {}
}

遍历set集合

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

WeakSet()

{
    let weaklist=new WeakSet();
    //支持的数据类型必须是对象,是地址的引用,不会检测对象是不是被回收
    let arg={};
    weaklist.add(arg);
    console.log(weaklist)
    //WeakSet {{}}
    weaklist.add(1)
    // Invalid value used in weak set
}

Map()

{
    let map=new Map();
    let arr=['123'];
    //map添加元素用set
    map.set(arr,456);
    console.log(map,map.get(arr))
    //Map(1) {Array(1) => 456} 456
}
{
    let map=new Map([['a',123],['b',456]]);
    console.log(map)
    //Map(2) {"a" => 123, "b" => 456}
    console.log(map.size)
    //2
    console.log(map.delete('a'))
    //true
    map.clear()
    console.log(map)
}

WeakMap()

{
    let weakmap=new WeakMap();
    //key值必须是对象,和weakset基本一样
    let o={"a":"456"}
    weakmap.set(o,123);
    console.log(weakmap.get(o))
}

Map()和数组的横向对比,增删改查

{
    //数据结构横向对比
    let map=new Map();
    let array=[];
    //增
    map.set('t',1);
    array.push({"t":1});
    console.info(map,array)
    //Map(1) {"t" => 1} [{…}]
    //查
    let map_exist=map.has('t');
    let array_exist=array.find(item=>item)
    console.info(map_exist,array_exist)
    //true {t: 1}
    //改
    map.set('t',2);
    array.forEach(item=>item.t?item.t=2:'')
    console.info(map,array)
    //Map(1) {"t" => 2}  [{…}]
    //删除
    map.delete('t');
    let index=array.findIndex(item=>item.t);
    array.splice(index,1)
    console.info(map,array)
    //Map(0) {} []
}

Set()和数组的横向对比,增删改查

{
    let set=new Set();
    let array=[];
    //增
    set.add({t:1})
    array.push({t:1})
    console.info(set,array)
    //Set(1) {{…}} [{…}]
    //查
    let set_exist=set.has({t:1})
    let array_exist=array.find(item=>item)
    console.info(set_exist,array_exist)
    //false {t: 1}
    //改
    set.forEach(item=>item.t?item.t=2:'');
    array.forEach(item=>item.t?item.t=2:'')
    console.info(set,array)
    //Set(1) {{…}} [{…}]
    //删除
    set.forEach(item=>item.t?set.delete(item):'');
    let index=array.findIndex(item=>item.t);
    array.splice(index,1)
    console.info(set,array)
    //Set(0) {} []
}

Object,Map,Set的对比,增删改查

{
       let item={t:1};
       //let it=[['a',1],['b',2],['c',3]]
       let map=new Map();
       let set=new Set();
       let obj={};
       //增
    map.set('t',1);
    set.add(item);
    obj['t']=1;
    console.log(map,set,obj)
    //Map(1) {"t" => 1} Set(1) {{…}} {t: 1}
    //查
    console.log(map.has('t'),set.has(item),'t' in obj)
    //true true true
    //改
    map.set('t',2);
    item.t=2;
    obj['t']=2;
    console.log(map,set,obj)
    //Map(1) {"t" => 2} Set(1) {{…}} {t: 2}
    //删除
    map.delete('t');
    set.delete(item);
    delete obj['t'];
    console.log(map,set,obj)
    //Map(0) {} Set(0) {} {}
}

在开发过程中,能使用Map(),不使用数组,如果对数据结构要求存储的唯一性,那么使用Set();在存储数据时,放弃使用数组和object。

你可能感兴趣的:(ES-6--数据结构)