ES6-Set数据结构用法(详细版)

ES6-Set数据结构

Set

  1. Set数据结构

    =>Set数据结构类似于数组,但是与数组不同的是,Set中的成员都是唯一的、无序的、并且没有重复。

  2. Set实例的属性和方法

    • set.size 获取set数据结构的值的个数
      const set = new Set([1,2,3,4]);
      console.log(set.size);// 4
      
    • set.add( ) 向set数据结构中添加某个值 返回数据结构本身
      const set = new Set([1,2,3,4]);
      console.log(set.add(5));// Set(5) {1, 2, 3, 4, 5}
      
    • set.delete( ) 删除set数据结构中的某个值 返回一个布尔值表示是否删除成功
      const set = new Set([1,2,3,4,5]);
      console.log(set.delete(5));// true
      console.log(set.delete(5));// false
      
    • set.has( ) 表示该参数是否是set的成员 返回一个布尔值
      const set = new Set([1,2,3,4,5]);
      console.log(set.has(5));//true
      console.log(set.has(6));//false
      
    • set.clear( ) 清除所有set数据结构成员 没有返回值
       const set = new Set([1,2,3,4,5]);
       set.clear()
       console.log(set);//Set(0) {}
      
  3. Set数据结构的遍历

    • keys( ) 返回键名

      const set = new Set([1,2,3,4,5]);
          for(let item of set.keys()) {
              
              
              console.log(item);
          }// 1 2 3 4 5
      
    • values( ) 返回键值

       const set = new Set([1,2,3,4,5]);
          for(let item of set.values()) {
              
              
              console.log(item);
          }//1 2 3 4 5
      
    • entries( ) 返回键值对

      const set = new Set([1,2,3,4,5]);
          for(let item of set.entries()) {
              
              
              console.log(item);
          }
      //(2) [1, 1]
      //(2) [2, 2]
      //(2) [3, 3]
      //(2) [4, 4]
      
    • forEach( value,key ) 遍历每个成员,可以直接操作Set数据结构成员

      const set = new Set([1,2,3,4,5]);
          set.forEach(function(value,key) {
              
              
             console.log( item * 2);
          })
      
  4. Set数据结构的特点

    • Set函数可以接收一个数组作为参数来初始化,返回一个set数据结构

      const set = new Set(['a','b','c','c']);
      console.log(set); //Set(3) {"a", "b", "c"} 
      
    • Set成员的唯一性,在Set数据结构中每个成员都是唯一的,而且向Set加入值时不会发生类型转换,1和‘1’是两个不同的值,但是向Set中添加两个NaN时,我们都应该知道 默认情况下NaN !==NaN,但是在Set数据结构中只能添加一个NaN,也就是说在Set数据结构中两个NaN被默认认为相等了,但是向Set数据结构中添加两个空对象则总是不想等的;

      //1 
      const set = new Set(['a','b','c','c']);
          set.add(NaN);
          set.add(NaN);
          console.log(set);//Set(4) {"a", "b", "c", NaN}
      
      //2
      const set = new Set(['a','b','c','c']);
          set.add({
              
              });
          set.add({
              
              });
          console.log(set);//Set(5) {"a", "b", "c", {…}, {…}}
      //3
      const set = new Set(['a','b','c','c']);
          set.add(1);
          set.add('1');
          console.log(set);//Set(5) {"a", "b", "c", 1, "1"}
      
  5. 将Set数据结构转换成数组的两种方法

    • Array.from( set ) 使用Array构造函数的静态方法
      const set = new Set([1,2,3,4,5]);
      console.log(set);//Set(5) {1, 2, 3, 4, 5}
      console.log(Array.from(set));//(5) [1, 2, 3, 4, 5]
      
    • 利用扩展运算符(…)
      const set = new Set([1,2,3,4,5]);
      console.log(set);//Set(5) {1, 2, 3, 4, 5}
      console.log([...set]);//(5) [1, 2, 3, 4, 5]
      
  6. 利用Set数据结构数组去重

     let arr = [1,2,3,4,5,5,3,2,3,1];
        const set = new Set(arr);
        console.log([...set]);//5) [1, 2, 3, 4, 5]
    

猜你喜欢

转载自blog.csdn.net/chen_junfeng/article/details/109275904