es6-set,map

   //类似于数组  成员的值是唯一的    add()添加   
        //创建方式一: 
        const set = new Set();
        set.add(2).add('2').add(4);
        console.log(set);
        //创建方式二:
        var arr = [1,2,3,4,2,4,5,2,1,4,5,5,7];
        const set2 = new Set(arr);
        //创建方式三:
        const set3 = new Set();
        [2,34,2,4,5,2,4].forEach(item => set3.add(item));

        //size   长度   去重后的长度
        set.size

        //delete()   删除
        set.delete(3);
        //clear()   清除所有

        //has()  是否set成员   返回true   false
        set.has(1)

        const set = new Set();
        set.add(2).add('a').add(4);
        var a = 'a';
        if(set.has(a)){
            console.log('ok');
        };

        //数组去重  转为数组
        //一种写法
        var arr = [1,2,3,4,2,4,5,2,1,4,5,5,7];
        console.log([...new Set(arr)]);
        //二种写法
        var arr2 = new Set([1,2,3,4,2,4,5,2,1,4,5,5,7]);
        [...arr2];

        //三种写法  Array.from()
        var arr2 = new Set([1,2,3,4,2,4,5,2,1,4,5,5,7]);
        var arr3 = Array.from(arr2);

        //循环    for  of        for  in
        //for...of  拥有者 array  strings   set  map     (iterator方法)
        //for...in
        //对象中获取的是key
        var obj = {name:1,age:20};
        for(var v in obj){
            console.log(v)   //name   age  console.log(obj[v])  ====1   20
        };
        //数组中获取的是索引
        var arr = [1,2,3,4,5];
        for(var v in arr){
            console.log(v)   //01234索引  console.log(arr[v])  1,2,3,4,5
        };
        // for ..of  获取当前的值
        var arr = [1,2,3,4,5];
        for(var v of arr){
            console.log(v);
        };
        //字符串
        var str = 'hello';
        for(var v of str){
            console.log(v);
        };

        //set
        var arr2 = new Set([1,2,3,4,2,4,5,2,1,4,5,5,7]);
        for(let v of arr2){
            console.log(v);
        };

        //遍历 方法   keys()   value()  entries()
        var arr2 = new Set([1,2,3,4,2,4,5,2,1,4,5,5,7]);
        for(let v of arr2.entries()){
            console.log(v);
        };

        //filter()  过滤方法
        var a = [1,2,3,4,2,4,5,2,1,4,5,5,7].filter(function(item,index){
            return item >3
        });
        //获取交集
        var arr1 = new Set([1,2,3,4,2,4,5,2,1,4,5,5,7]);
        var arr2 = new Set([9,5,2,4,8]);
        var arr3 = new Set([...arr1].filter(item => arr2.has(item)));  // 2 4 5
        //并集
        var arr4 = new Set([...arr1,...arr2]);
        //差集
        var arr1 = new Set([1,2,3,4,2,4,5,2,1,4,5,5,7]);
        var arr2 = new Set([9,5,2,4,8]);
        var arr3 = new Set([...arr1].filter(item => !arr2.has(item)));  //1 3 7
        var arr3 = new Set([...arr2].filter(item => !arr1.has(item)));  //9 8

        //map   类似于对象   map的键可以是其它的类型   键值对
        var m = new Map()
        m.set('name','abc').set('age',20);
        var name = 'name';
        m.get(name);
        //可以为其它类型
        m.set(123,'123');
        m.set(true,'124');
        m.set(undefined,'124');
        var a = {msg:'hello'};
        m.set(a,'name');
        m.get(a);

        //has()
        //delete()
        //clear()
        //size

        // Map 可以接受一个数组作为参数
        var m = new Map([['name','a'],['age',10],['a',3]]);
        //keys()   value()  entries()
        for(let [k,v] of m){
            console.log(v)
        };
        for(let k of m.keys()){
            console.log(k)
        };
        for(let v of m.values()){
            console.log(v)
        };

        //扩展运算符
        var m = new Map([['name','a'],['age',10],['a',3]]);
        var m2 = [...m.keys()];  //["name", "age", "a"]
        var m3 = [...m.value()];  //["a", 10, 3]

        //类型转换
        var m = new Map([['name','a'],['age',10],['a',3]]);
        //map转为数组
        [...m];  
        [...m.keys()]; 
        [...m.value()];
        //数组转为map
        var m = new Map([['name','a'],['age',10],['a',3]]);
        var m2 = [...m];  
        var m3 = new Map(m2);  
        //转为对象
        var m = new Map([['name','a'],['age',10],['a',3]]);
        var obj = {};
        for(let [k,v] of m){
            obj[k] = v;
        };
        console.log(obj);
        //对象转为map
        var obj2 = {msg:'hello',flag:true};
        var m = new Map();
        for(let k in obj2){
            m.set(k,obj2[k])
        };

        //4、另一种方式  对象转map
		function objToStrMap(obj) {
		  let m = new Map();
		  for (let k of Object.keys(obj)) {    //name value   for...of 不能遍历对象
			m.set(k, obj[k]);
		  }
		  return m;
		};
		objToStrMap({name:1, value:2});

        //Object.keys()
        var obj2 = {msg:'hello',flag:true};
        console.log(Object.keys(obj2));  //["msg", "flag"]
        console.log(Object.values(obj2));  //["hello", true]


    }

 

阅读更多
换一批

没有更多推荐了,返回首页