浏览量:200次
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;i7、交集
Set.prototype.intersection=function(otherSet){ var intersectionSet=new Set() var values=this.values() for(var i=0;i8、补集
Set.prototype.difference=function(otherSet){ var differenceSet=new Set() var values=this.values() for(var i=0;i9、子集
Set.prototype.subset=function(otherSet){ var values=this.values() for(var i=0;i
[声明]本网转载网络媒体稿件是为了传播更多的信息,此类稿件不代表本网观点,本网不承担此类稿件侵权行为的连带责任。故此,如果您发现本网站的内容侵犯了您的版权,请您的相关内容发至此邮箱【779898168@qq.com】,我们在确认后,会立即删除,保证您的版权。