zoukankan      html  css  js  c++  java
  • Javascript中的集合

    集合是由一组无序且唯一(即不能重复)的项组成

    function Set() {
            var items={};
            this.has=function(value){
              //return value in items;
              return this.hasOwnProperty(value);
            }
            this.add=function (value) {
              if(!this.has(value)){
                items[value]=value;
                return true;
              }
              return false;
            }
            this.remove=function(value){
              if(this.has(value)){
                delete items[value];
                return true;
              }
              return false;
            }
            this.clear=function(){
              items={};
            }
            this.size=function(){
              return Object.keys(items).length;
            }
            this.sizeLegacy=function(){
              var count=0;
              for (var prop in items) {
                if (items.hasOwnProperty(prop)) {
                  ++count;
                }
              }
            }
            this.values=function () {
              return Object.keys(items);
            }
            this.valuesLagacy=function () {
              var keys=[];
              for (var key in items) {
                if (items.hasOwnProperty(key)) {
                    keys.push(key);
                }
              }
              return keys;
            }
            //并集
            this.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; i++) {
                unionSet.add(values[i]);   
              }
              return unionSet;
            }
            // 交集
            this.intersection=function (otherSet) {
              var intersectionSet=new Set();
              var values=this.values();
              for (var i = 0; i < values.length; i++) {
                if (otherSet.has(values[i])) {
                  intersectionSet.add(values[i])
                }
              }
              return intersectionSet;
            }
            // 差集
            this.difference=function(otherSet){
              var differenceSet=new Set();
              var values=this.values();
              for (var i = 0; i < values.length; i++) {
                if (!otherSet.has(values[i])) {
                  differenceSet.add(values[i]);
                }
              }
              return differenceSet;
            }
            // 子集
            this.isSubsetOf=function(otherSet){
              if(this.size()>otherSet.size()){
                return false;
              }else{
                var values=this.values();
                for (var i = 0; i < values.length; i++) {
                  if(!otherSet.has(values[i])){
                    return false;
                  }
                }
                return true;
              }
            }
    }
    

      

  • 相关阅读:
    错误: 找不到或无法加载主类/java BB.class 中文乱码
    如何新建git仓库并连接以及git branch相关命令
    String、StringBuffer与StringBuilder区别
    HTML选择器
    java中的system.out.println()和JSP中out.println()区别
    校园双选会,你都懂么
    android visible invisible和gone的区别
    Android 显示意图和隐式意图的区别
    android doGet和doPost
    Android 监听SMS短信
  • 原文地址:https://www.cnblogs.com/BaiGuodong/p/javascript-collection-set.html
Copyright © 2011-2022 走看看