• ES6数据解构:set map 的介绍


    set:类似于数组,但它的一大特性就是所有元素都是唯一的,没有重复。我们可以利用这一唯一特性进行数组的去重工作。

      单一数组的去重:

        let set = new Set([1, 2, 2, 3, 4, 3, 5])
        console.log(set) //Set(5) {1, 2, 3, 4, 5}
        console.log(set.size)//5  set没有length属性 要用size属性

      多数组的合并去重:不会进行数据类型的转换

        let arr1 = [1, 2, 3, 4, '5']
        let arr2 = [2, 3, 4, 5, 6]
        let set7 = new Set([...arr1, ...arr2])
        console.log(set7) //Set(6) {1, 2, 3, 4, '5', 5, 6}

      向set中添加元素:

        let set1 = new Set()
        set1.add(1)
        set1.add(2)
        set1.add(3)
        console.log('added:', set1)//Set(3) {1, 2, 3}

      set中删除元素:

        let set1 = new Set([1,2,3,4,5])
        set1.delete(1)
        console.log('deleted:', set1)//Set(4) {2, 3, 4, 5}

      set中清除所有元素:

        let set1 = new Set([1,2,3,4,5])
        set1.clear()
        console.log(set1)//Set(0) {}

      set中判断某元素是否存在:

        let set = new Set([1,2,3,4,5])
        console.log( set.has(1))//true
        console.log( set.has(8))//false

      遍历set:

        let arr=['add','delete','clear','has'];
        let list=new Set(arr);
        let str = str1 = '0';
        for(let key of list){
            str += key + ' ';
        }
        console.log(str)//add delete clear has
        list.forEach(function(item){
            str1 += item + ' ';
        })
        console.log(str1)//add delete clear has

      Set和Array互转:

        let arr = [1,2,3,4,5];
        //数组转set
        let set = new Set(arr)
        console.log(set)//Set(5) {1, 2, 3, 4, 5}
        //set转数组有两种方案:
        console.log([...set])
        console.log( Array.from(set))

    map特性:

      1.键值对,键可以是任何数据类型包括对象。

        let map = new Map()
        let objkey = {p1: 'v1'}
        map.set(objkey, 'hello')//添加
        console.log(map)//Map(1) { {p1: "v1"} => "hello" }
        console.log(map.get(objkey))//hello

      2.Map可以接受数组作为参数,数组成员还是一个数组,其中有两个元素,一个表示键一个表示值。

        let map = new Map([
            ['name', 'Aissen'],
            ['age', 12]
        ])
        console.log(map);//Map(2) {"name" => "Aissen", "age" => 12}

      map的属性和方法:

        let map = new Map([
            ['name','小明'],
            ['age',16],
            ['hobby','打游戏']
        ])
        console.log(map) //Map(3) {"name" => "小明", "age" => 16, "hobby" => "打游戏"}
        console.log(map.size)// 3 获取map的长度 用size map没有length属性
        console.log(map.get('hobby'))//打游戏
        //判断是否存在指定的键
        console.log(map.has('hobby')) // true
        console.log(map.has('hello')) // false
        //添加 修改
        map.set('sex','男')
        console.log(map) // Map(4) {"name" => "小明", "age" => 16, "hobby" => "打游戏", "sex" => "男"}
        //删除
        map.delete('hobby')
        console.log(map)//Map(3) {"name" => "小明", "age" => 16, "sex" => "男"}
        //清空所有数据
        map.clear()
        console.log(map) //Map(0) {}

      遍历map:

        let map = new Map([
            ['name','小明'],
            ['age',16],
            ['hobby','打游戏']
        ])
        for(let key of map.keys()){
            console.log(key) //各遍历出map的键名 name age hobby
        }
        for(let value of map.values()){
            console.log(value) //各遍历出map的键值 小明 16 打游戏
        }
        for(let [key,value] of map.entries()){
            console.log(key,value)//各遍历出map的键值对 name 小明 | age 16 | hobby 打游戏
        }

      map和其他数据类型互转:

      和数组的互转:

        let map = new Map([
            ['name','小明'],
            ['age',16],
            ['hobby','打游戏']
        ])
        let arr = [...map];
        console.log(arr)//转成二维数组 [ ["name", "小明"], ["age", 16],  ["hobby", "打游戏"] ]
        let map1 = new Map(arr)
        console.log(map1)//Map(3) {"name" => "小明", "age" => 16, "hobby" => "打游戏"}

      和Object的互转:

        let map = new Map([
            ['name','小明'],
            ['age',16],
            ['hobby','打游戏']
        ])
        let obj = {};
        for (let [k,v] of map) {
            obj[k] = v;
        }
        console.log(obj) //{name: "小明", age: 16, hobby: "打游戏"}
        let map1 = new Map();
        for(let key in obj){
            map1.set(key,obj[key])
        }
        console.log(map1)//Map(3) {"name" => "小明", "age" => 16, "hobby" => "打游戏"}
  • 相关阅读:
    转:iphone 申请证书
    [C#] Window Form 事件順序
    探讨PHP获取checkbox值
    PHP判断字符串的包含
    PCCSRGB变换表
    21世纪初最有影响力的20篇计算机视觉期刊论文
    Win7 64bit下MexOpenCV的安装,Matlab和C++&OpenCV的完美结合
    Comics: do we know that we are not doing research in the wrong way?
    Deep Learning at NIPS2012
    21世纪初最有影响力的30篇计算机视觉会议论文
  • 原文地址:https://www.cnblogs.com/rickyctbur/p/11556223.html
Copyright © 2020-2023  润新知