1.setのセットを定義
コレクションのメンバーが順序付けられていない、グループメンバーの繰り返しではありません。
開発は、重複データを削除するために使用することができます
セットコレクションとマップは同じではありません。ここでは唯一の方法コレクションセットを実装します。
ハッシュマップ構造は、だけでなく、配列の性質と結合鎖に達成するために定義されています。
この方法が導入されていない、セットの数学的な定義が学習されます。
2.パッケージオブジェクト
ここでは、設定した目標を達成するための方法です
function Set(){
this.items={}
}
3.新しい付加価値
その名の健康と健康値のデフォルトセット
Set.prototype.add=function(value){
if(this.has(value)){
return false
}
this.items[value]=value
return true
}
3.値を削除します。
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
}
4.一般的な手順
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)
}
5.設定し、
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
}
6.交差点
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
}
7.補数
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
}
8.子集
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
}