js Array 交集 并集 差集 去重

1、差集

//2个集合的差集 在arr不存在
Array.prototype.minus = function (arr) {
    var result = new Array();
    var obj = {};
    for (var i = 0; i < arr.length; i++) {
        obj[arr[i]] = 1;
    }
    for (var j = 0; j < this.length; j++) {
        if (!obj[this[j]])
        {
            obj[this[j]] = 1;
            result.push(this[j]);
        }
    }
    return result;
};

2、并集
不去重,并附加到第一个数组上

var properthname = [1,2,3]
var goonproperthname = [2,3,4]         //继续附加的属性名称
var mergeproperthname = $.merge(properthname,goonproperthname);
console.log(properthname);      //[1,2,3,2,3,4]    == 附加到第一个数组上
console.log(goonproperthname);  //[2,3,4]
console.log(mergeproperthname); //[1,2,3,2,3,4]

猜你喜欢

转载自blog.csdn.net/eddy23513/article/details/80624354