• js封装一个集合


    // 基于对象封装一个集合
    function Set() {
        // 属性
        this.items = {};
        // ------------------方法-----------------
        // add  往集合中添加元素
        Set.prototype.add = function (value) {
            // 先判断是否有这个元素
            if (this.has(value)) return false;
            // 没有的话就添加
            this.items[value] = value;
            return true
        }
        // has  判断集合中是否已经有了这个元素
        Set.prototype.has = function (value) {
            return this.items.hasOwnProperty(value)
        }
        // remove  删除集合中的这个元素
        Set.prototype.remove = function (value) {
            // 先判断是否有这个元素
            if (!this.has(value)) return false;
            // 有的话删除
            delete this.items[value];
            return true
        }
        // clear    删除集合中的所有元素
        Set.prototype.clear = function(){
            this.items = {};
            return true
        }
        // size    获取集合的长度
        Set.prototype.size = function(){
            return Object.keys(this.items).length
        }
        // values   获取集合中的所有元素
        Set.prototype.values = function(){
            return Object.values(this.items)
        }
        //  -----集合间的操作----
        // 并集
        Set.prototype.union = function(otherSet){
            // this  代表集合A
            // otherSet  代表集合B
            // 1.先创建一个unionSet
            let unionSet = new Set()
            // 2.将集合A中的元素放到unionSet中
            var values = this.values();
            for(let i = 0; i < values.length; i++){
                unionSet.add(values[i])
            }
            // 3.判断集合B中的元素在unionSet中是否存在,没有的话就加进去
            values = otherSet.values();
            for(let i = 0; i < values.length; i++){
                unionSet.add(values[i])
            }
            // 4.返回unionSet集合
            return unionSet
        }
        // 交集
        Set.prototype.intersection = function(otherSet){
            // 1.创建一个新的集合
            let intersectionSet = new Set();
            // 2.遍历集合A,判断其元素在集合B中是否存在,存在就存到交集集合中
            var values = this.values();
            for(let i = 0; i < values.length; i++){
                if(otherSet.has(values[i])){
                    intersectionSet.add(values[i])
                }
            }
            return intersectionSet
    
        }
        // 差集
        Set.prototype.difference = function(otherSet){
            // 1.创建一个新的集合
            let differenceSet = new Set();
            // 2.遍历集合A,判断其元素在集合B中是否存在,不存在就存到差集集合中
            var values = this.values();
            for(let i = 0; i < values.length; i++){
                if(!otherSet.has(values[i])){
                    differenceSet.add(values[i])
                }
            }
            return differenceSet
        }
        // 子集  判断A是否是B的子集
        Set.prototype.subSet = function(otherSet){
            // 遍历集合A,判断其元素在集合B中是否存在,只要有一个不存在就代表A不是B的子集
            var values = this.values();
            for(let i = 0; i < values.length; i++){
                if(!otherSet.has(values[i])){
                    return false
                }
            }
            return true
        }
    
    
    }
    
    
    // ----------------------测试-------------------
    // let set = new Set();
    // set.add(12);
    // set.add('好看');
    // set.add(34);
    // set.add('美女');
    // console.log(set);
    // console.log(set.values());
    // console.log(set.size());
    // console.log(set.has(34));
    // console.log(set.has(00));
    // console.log(set.remove(34));
    // console.log(set.values());
    // console.log(set.clear());
    // console.log(set.values());
    
    let setA = new Set();
    setA.add(1);
    setA.add(2);
    setA.add(3);
    let setB = new Set();
    setB.add(3);
    setB.add(4);
    setB.add(5);
    let setC = new Set();
    setC.add(1);
    setC.add(2);
    console.log(setA.union(setB).values());
    console.log(setA.intersection(setB).values());
    console.log(setA.difference(setB).values());
    console.log(setC.subSet(setA));   // true   C是A的子集
    console.log(setA.subSet(setB));   // false   A不是B的子集
  • 相关阅读:
    jsp自定义标签
    用javascript获取屏幕高度和宽度等信息
    解决document.location.href下载文件时中文乱码
    centos7下的ifconfig命令未安装
    vmstat命令
    FPM打包工具使用
    nmap的使用
    检测硬件RDMA卡是否存在
    RDMA卡的检测方法
    硬件RDMA的驱动配置和测试
  • 原文地址:https://www.cnblogs.com/cyf666cool/p/14837226.html
Copyright © 2020-2023  润新知