<!DOCTYPE html> <html> <head> <title>集合</title> <meta charset="utf-8"> <script type="text/javascript"> function Set(){ let items = {}; // add:添加一个新的项 this.add = function(value){ if(!this.has(value)){ // 添加一个值的时候,把它同时作为键和值保存,有利于查找这个值 items[value] = value; return true; } return false; }; // delete:删除值 this.delete = function(value){ if(this.has(value)){ delete items[value]; return true; } return false; }; // has:如果值在集合中返回true,否则返回false this.has = function(value){ return items.hasOwnProperty(value); }; // clear:移除集合中的所有项 this.clear = function(){ items = {}; }; // size:返回集合中所包含的元素数量,与数组的length属性类似 this.size = function(){ return Object.keys(items).length; }; // sizeLegacy:跨浏览器兼容返回集合元素数量 this.sizeLegacy = function(){ let count = 0; for(let key in items){ if(items.hasOwnPrototype(key)){ ++count; } } return count; }; // values:返回一个包含集合中所有数据的数组 this.values = function(){ let values = []; for(let i=0, keys = Object.keys(items); i<keys.length; i++){ values.push(items[keys[i]]); } return values; }; // valuesLegacy:跨浏览器兼容 this.valuesLegacy = function(){ let values = []; for(let key in items){ if(items.hasOwnPrototype(key)){ values.push(items[key]); } } return values; }; // getItems this.getItems = function(){ return items; }; // union:并集 this.union = function(otherSet){ let unionSet = new Set(); let values = this.values(); for(let i=0; i<values.length; i++){ unionSet.add(values[i]); } values = otherSet.values(); for(let i=0; i<values.length; i++){ unionSet.add(values[i]); } return unionSet; }; // intersection:交集 this.intersection = function(otherSet){ let intersectionSet = new Set(); let values = this.values(); for(let i=0; i<values.length; i++){ if(otherSet.has(values[i])){ intersectionSet.add(values[i]); } } return intersectionSet; }; //difference:差集 this.difference = function(otherSet){ let differenceSet = new Set(); let values = this.values(); for(let i=0; i<values.length; i++){ if(!otherSet.has(values[i])){ differenceSet.add(values[i]); } } return differenceSet; }; // subset:子集 this.subset = function(otherSet){ if(this.size() > otherSet.size()){ return false; } else{ let values = this.values(); for(let i=0; i<values.length; i++){ if(!otherSet.has(values[i])){ return false; } } } return true; } } // Set的使用 var set1 = new Set(); set1.add(1); set1.add(2); set1.add(3); var set2 = new Set(); set2.add(1); set2.add(2); set2.add(3); set2.add(4); set2.add(5); var set3 = new Set(); set3.add("a"); set3.add("b"); set3.add("c"); set3.add("d"); // 打印集合大小 console.log(set1.size()); //3 console.log(set2.size()); //5 console.log(set3.size()); //4 //打印集合元素的数组形式 console.log(set1.values()); //[1,2,3] console.log(set2.values()); //[1,2,3,4,5] console.log(set3.values()); //["a", "b", "c", "d"] // 求set1和set2的并集 let set12 = set1.union(set2); console.log(set12.values()); //[1,2,3,4,5] // 求set1和set2的交集 let _set12 = set1.intersection(set2); console.log(_set12.values()); //[1,2,3] // 求set1和set2的差集 let set2_1 = set2.difference(set1); console.log(set2_1.values()); //[4,5] // 判断set1是否为set2的子集 console.log(set1.subset(set2)); //true // 判断set3是否为set2的子集 console.log(set3.subset(set2)); //false </script> </head> <body> </body> </html>