js 中的Set,Map

Map

Map(映射)是一种可迭代的键值对(key/value)结构。
所有的值都可以通过键来获取。
Map 中的键都是唯一的。

let map = new Map();

      map.set("title", "understaing"); //'title'是Map的键名,‘understaing是值
      map.set("data", "5");
      console.log(map);
      console.log(map.get("title")); //get()方法是获取键名对应的值,如果不存在返回undefined
      console.log(map.get("data")); //5  和set集合不同会改变元素类型

      let key1 = {},
        key2 = {};
      map.set(key1, "5");
      console.log(map.get(key1));

      //与set集合相同也拥有has,delete,clear方法,检测key值,size,检测长度

      map.delete(key1);
      console.log(map.has(key1));
      console.log(map.size);
      map.clear();
      console.log(map);

      //Map集合的初始化
      let map1 = new Map([
        ["name", "wang"],
        ["age", "20"],
      ]);

      console.log(map1.get("name", "age")); //get方法仍然是只能获取一个元素的值,且为第一个元素
      console.log(map1.get("age"));

      //Map 的forEach方法和Set的forEach方法相同

      map1.forEach(function (value, key, ownerMap) {
        console.log(key + " " + value);
        console.log(ownerMap);
      });

Set

Set 是一种新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值。
它本身便是一个构造函数用来生成数据结构。

//Set类型不会对存值的强制转换,基本上是都已独立元素存在,除了,-0和+0
      let set = new Set();
      set.add(5); //添加元素 ==>普通数组添加元素方法push,unshift,splice
      set.add("5");
      set.add(5); //重复调用则被忽略
      console.log(set.size); //size  属性是访问set中元素数量

      let set1 = new Set(),
        key1 = { name: 1 },
        key2 = { name: 2 };
      set1.add(key1);
      set1.add(key2);
      console.log(set1); //key1,key2不会转换成字符串,因此,他们显示的Object

      let set2 = new Set([1, 2, 3, 5, 5, 5, 4, 4, 3]);
      console.log(set2.size); //Set自动去除数组中的重复     ==>普通数组去除元素重复需要遍历,排序,然后去除相同元素

      let set3 = new Set();
      set3.add(5);
      set3.add("5"); //add方法每次只能增添一个元素,且为第一个
      console.log(set3.has(5)); //true  has方法检测数组中是否存在某值
      console.log(set3.has(6));

      let arr = [1, 2, 3, 4, 5];
      console.log(arr.has); //has方法只能用于Set创建的集合
      //indexOf或unindexOf检测字符串元素位置,includes检测数组中是否存在某值

      let set4 = new Set();
      set4.add(4);
      set4.add(5);
      set4.delete(5); //delete移除set数组中的元素去除指定元素    ==>普通数组移除元素方法 pop(),shift(),splice
      console.log(set4.has(5));

      set4.add(6);
      set4.clear(); //直接清除set4中的所有元素
      console.log(set4.size); //0

      //forEach()方法遍历
      let set5 = new Set([1, 2]);
      set5.forEach(function (value, key, ownerSet) {
        console.log(key + " " + value);
        console.log(ownerSet);
      });

      let set6 = new Set([1, 2]);
      let processor = {
        output(value) {
          console.log(value);
        },
        process(dataSet) {
          dataSet.forEach(function (value) {
            this.output(value);
          }, this);
        },
      };
      console.log(processor.process);

      //Set集合转换为数组
      let set7 = new Set([1, 2, 3, 4, 4, 3, 5]),
        array = [...set7];
      console.log(array);

      //利用Set去除数组重复
      let arr2 = [1, 2, 3, 4, 5, 4, 3, 2, 34];
      let set8 = [...new Set(arr2)];
      console.log(set8);

本文链接:http://www.wangcanghai.cn/detailes?id=601b706524fc4a133c24648c
点击支持一下博主:嘿嘿

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值