第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

JavaScript數(shù)據(jù)結(jié)構(gòu)之創(chuàng)建一個(gè)集合、集合操作

集合是由一组无序且唯一(即不能重复)的项组成的。可以把集合想象成一个既没有重复元素,也没有顺序概念的数组。

声明一些集合可用的方法:
    has(value):如果值在集合中,返回true,否则返回false;
    add(value):向集合添加一个新的项;
    remove(value):从集合移除一个值;
    clear():移除集合中的所有项;
    size():返回集合中包含元素的数量;
    values():返回一个包含集合中所有值的数组;

声明一些集合可用的方法:
    union():并集;
    intersection():交集;
    difference():差集;
    subset():子集:

function Set() {
        //这里使用对象而不是数组来表示集合(items),但也可以用数组来实现
        var items = {}; 

        //has(value)方法 
        this.has = function(value) {
            //所有的JavaScript对象都有hasOwnProperty方法,返回表明对象是否具有特定属性的布尔值
            return items.hasOwnProperty(value);
        };

        //add方法
        this.add = function(value) {
            if (!this.has(value)) { //检查给定的value是否存在于集合中,如果不存在就添加
                items[value] = value;
                return true;
            }
            return false; //如果集合中已经有了这个值,就返回false
        };

        //remove方法
        this.remove = function(value) {
            if (this.has(value)) {
                //如果验证给定的value存在于集合中,那就就移除value
                delete items[value];
                return true; //表示值被移除
            }
            return false;
        };

        //clear方法
        this.clear = function() {
            //要重置对象,只需把一个空对象重新赋值给它
            items = {};
        };

        //size()方法
        this.size = function() {
            //JavaScript的Object类有一个keys方法,它返回一个包含给定对象所有属性的数组
            return Object.keys(items).length;
        };

        //values方法
        this.values = function() {
            //提取items对象的所有属性
            return Object.keys(items);
        };

        //union方法
        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;
        };

        //intersection方法
        this.intersection = function(otherSet) {
            var intersectionSet = new Set();

            var values = this.values();
            for (var i = 0; i < values.length; i++) {
                //遍历当前实例中所有的值,验证它们是否也存在于otherSet实例中
                if (otherSet.has(values[i])) {
                    intersectionSet.add(values[i]);
                }
            }
            return intersectionSet;
        };

        //difference方法
        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;
        };

        //subset方法
        this.subset = 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;
            }
        };

    }

测试上面代码:

 var setA = new Set();
    setA.add(1);
    setA.add(2);
    setA.add(3);
    setA.add(4);
    var setB = new Set();
    setB.add(3);
    setB.add(4);
    setB.add(5);
    setB.add(6);
    var setC = new Set();
    setC.add(1);
    setC.add(2);
    setC.add(3);
    setC.add(4);
    setC.add(5);

    var unionAB = setA.union(setB);
    console.log(unionAB.values());

    var intersectionAB = setA.intersection(setB);
    console.log(intersectionAB.values());

    var differenceAB = setA.difference(setB);
    console.log(differenceAB.values());

    console.log(setA.subset(setB));
    console.log(setA.subset(setC));
點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消