zoukankan      html  css  js  c++  java
  • set与weakset

    set

    • 新增集合数据类型

    • 其中的数据都是唯一的

    • 不能通过索引获取值

    • 通过new Set()创建,可以直接将数组传入

    • 不同于对象,向其中添加数字类型数据和字符串类型数据是不一样的

    API

    • add() 向set类数组对象中加入元素,返回新生成的set

    • delete() 从set类数组对象中删除指定元素,返回布尔值

    • has() 判断set类数组对象中是否含有指定元素,返回布尔值

    • clear() 清除set类数组对象中的所有元素,返回undefined

    • size 获得set类数组对象的元素个数

    通过for...of... 遍历

    const clothes = new Set(['skirt', 'T-shirt', 'coat']);
    for (const item of clothes) {
       console.log(item)
    }

    通过forEach遍历

    • 为了和数组中的forEach()统一,同样需要传入回调函数

      • 回调函数中的参数,参数1与参数2相同

        • 参数1:属性名

        • 参数2:属性值

        • 参数3:调用的数组

        clothes.forEach((key, value, ownarr) => {
           console.log(key, value, ownarr)
        })

    通过set对象.value()获得Iterator接口

    • 通过调用next()遍历set

      const Clothes= clothes.values()
      undefined
      Clothes.next()
      {value: "skirt", done: false}
      Clothes.next()
      {value: "T-shirt", done: false}
      Clothes.next()
      {value: "coat", done: false}
      Clothes.next()
      {value: undefined, done: true}

    WeakSet

    • 其中的元素只能是对象

    • 因为没有Iterator接口,所以不能通过forEachfor of 遍历

    • 没有clear()方法,但是可以自动清除

      • 只要将引用的某个元素更改为null

  • 相关阅读:
    Global Citizenship
    Eng Stu
    说说
    C#编程远程控制机械手臂
    切割系统
    C#编码 画图控件
    编程Sourceforge
    C#编程线程
    空间点的几何关系
    一台普通电脑通过设置连接到公司网络
  • 原文地址:https://www.cnblogs.com/ashen1999/p/12559875.html
Copyright © 2011-2022 走看看