视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
python 遍历列表 vue babel javascript function js setattribute vue.set()用法 python中def函数的用法 vue获取路由参数 oracle netsuite python assert函数用法 python getattr函数 vue 获取当前路由 oracle expdp hp台式机bios设置启动顺序 最简单施工日志范文 oracle触发器写法 linux密码忘记 重置密码的方法 vue获取当前路由 echarts教程 oracle 添加索引 vue路由模式 js callback object.values()的用法 mysql text类型 vue main.js js lastindexof mysql 替换函数 js endswith js assign js queryselector mysql json_extract 16进制颜色转换rgb valueerror是什么错误 python九九乘法表代码 js object.assign tinfoil使用教程 0x80240017未指定错误 input获取焦点 图像采集是什么意思 浏览器cookie要不要打开 electron打包成exe
js set集合
2022-01-22 16:28:03 责编:李赢赢
文档

js中set集合是什么呢?不知道的小伙伴来看看小编今天的分享吧!

1、set集合的定义

 集合成员是无序的,是不重复的一组成员。

开发中可用于去除重复数据

set集合和map不一样。这里只实现了set集合的方法。

map是用哈希结构的定义来实现的,本质上也是对数组和链的结合。

2、封装对象 

        此处用对象的方式来实现集合
 function Set(){
        this.items={}
}

 3、新增值

    默认set的健名是其健值
 Set.prototype.add=function(value){
            if(this.has(value)){
                return false
            }
 
            this.items[value]=value
            return true
        }

4、删除值

  Set.prototype.has=function(value){
            return this.items.hasOwnProperty(value)
        }
 
        Set.prototype.remove=function(value){
            if(!this.has(value)){
                return false
            }
            delete this.items[value]
            return true
        }

5.一般方法

 Set.prototype.clear=function(){
            this.items={}
        }
        Set.prototype.size=function(){
            return Object.keys(this.items).length
        }
 
        Set.prototype.values=function(){
            return Object.keys(this.items)
        }

6、并集 

 Set.prototype.union=function(otherSet){
            var unionSet=new Set()
            var values=this.values()
            for(var i=0;i<values.length;i++){
                unionSet.add(values[i])
            }
            values=otherSet.values()
            for(var i=0;i<values.length;o++){
                unionSet.add(values[i])
            }
            return unionSet
        }

 7、交集

Set.prototype.intersection=function(otherSet){
            var intersectionSet=new Set()
            var values=this.values()
            for(var i=0;i<values.length;i++){
               var item=values[i]
               if(otherSet.has(item)){
                   intersectionSet.add(item)
               }
            }
           
            return intersectionSet
        }

8、补集

  Set.prototype.difference=function(otherSet){
            var differenceSet=new Set()
            var values=this.values()
            for(var i=0;i<values.length;i++){
               var item=values[i]
               if(!otherSet.has(item)){
                differenceSet.add(item)
               }
            }
           
            return differenceSet
        }

9、子集 

 Set.prototype.subset=function(otherSet){
            
            var values=this.values()
            for(var i=0;i<values.length;i++){
               var item=values[i]
               if(!otherSet.has(item)){
                return false
               }
            }
           
            return true
        }

以上就是小编今天的分享了,希望可以帮助到大家。

下载本文
显示全文
专题python 遍历列表python 遍历列表专题vue babelvue babel专题javascript functionjavascript function专题js setattributejs setattribute专题vue.set()用法vue.set()用法专题python中def函数的用法python中def函数的用法专题vue获取路由参数vue获取路由参数专题oracle netsuiteoracle netsuite专题python assert函数用法python assert函数用法专题python getattr函数python getattr函数专题vue 获取当前路由vue 获取当前路由专题oracle expdporacle expdp专题hp台式机bios设置启动顺序hp台式机bios设置启动顺序专题最简单施工日志范文最简单施工日志范文专题oracle触发器写法oracle触发器写法专题linux密码忘记 重置密码的方法linux密码忘记 重置密码的方法专题vue获取当前路由vue获取当前路由专题echarts教程echarts教程专题oracle 添加索引oracle 添加索引专题vue路由模式vue路由模式专题office2010配置进度怎么去除office2010配置进度怎么去除专题python excel写入数据python excel写入数据专题操作无法完成错误0x00000709操作无法完成错误0x00000709专题python将数据写入excel文件python将数据写入excel文件专题js urljs url专题vue router传参vue router传参专题jquery获取url参数jquery获取url参数专题vue router跳转vue router跳转专题clashx使用教程clashx使用教程专题vue publicpathvue publicpath专题js uml专题xpath w3c专题creat.js专题js 类库专题dojo.js专题dojo js专题jq sibling专题set获取第一个元素专题js 创建dom节点专题jquery.ztree.all.js专题js compose专题js range专题wizard.js专题js获取dom高度专题region.js专题trigger js专题js trigger专题filereader js专题jquery.datatables专题jquery datatables专题