js獲取兩個數組的合集和差集

打印兩個數組之間的差集
	var a = [110000,150000,310000];
	var b = [110000,310000];
	var c = [];
	var tmp = a.concat(b);
	var o = {};
	for (var i = 0; i < tmp.length; i ++) (tmp[i] in o) ? o[tmp[i]] ++ : o[tmp[i]] = 1;
	for (x in o) if (o[x] == 1) c.push(x);
	console.log(c);
//	結果是:150000
	
打印兩個數組之間的合集
	var arry1 = [1,2,3,4,5,6,7,8,9,0];
	var arry2 = [0,8,5,2,65];
	var arry3 = new Array();
	var j = 0;
	for(var i=0;i<arry1.length;i++){
	  for(var k=0;k<arry2.length;k++){
	   if(arry1[i]==arry2[k]){
	    arry3[j]=arry1[i];
	    ++j;
	   }
	  }
	}
	console.log(arry3);

使用 ES5 語法來實現雖然會麻煩些,但兼容性最好,不用考慮瀏覽器 JavaScript 版本。也不用引入其他第三方庫。

還有很多方法去實現下面一一例出

1,直接使用 filter、concat 來計算

var a = [1,2,3,4,5]
var b = [2,4,6,8,10]
 
//交集
var c = a.filter(function(v){ return b.indexOf(v) > -1 })
 
//差集
var d = a.filter(function(v){ return b.indexOf(v) == -1 })
 
//補集
var e = a.filter(function(v){ return !(b.indexOf(v) > -1) })
        .concat(b.filter(function(v){ return !(a.indexOf(v) > -1)}))
 
//並集
var f = a.concat(b.filter(function(v){ return !(a.indexOf(v) > -1)}));
 
console.log("數組a:", a);
console.log("數組b:", b);
console.log("a與b的交集:", c);
console.log("a與b的差集:", d);
console.log("a與b的補集:", e);
console.log("a與b的並集:", f);

2,對 Array 進行擴展

(1)爲方便使用,我們可以對數組功能進行擴展,增加一些常用的方法。

//數組功能擴展
//數組迭代函數
Array.prototype.each = function(fn){
  fn = fn || Function.K;
   var a = [];
   var args = Array.prototype.slice.call(arguments, 1);
   for(var i = 0; i < this.length; i++){
       var res = fn.apply(this,[this[i],i].concat(args));
       if(res != null) a.push(res);
   }
   return a;
};
 
//數組是否包含指定元素
Array.prototype.contains = function(suArr){
  for(var i = 0; i < this.length; i ++){
      if(this[i] == suArr){
          return true;
      }
   }
   return false;
}
 
//不重複元素構成的數組
Array.prototype.uniquelize = function(){
   var ra = new Array();
   for(var i = 0; i < this.length; i ++){
      if(!ra.contains(this[i])){
          ra.push(this[i]);
      }
   }
   return ra;
};
 
//兩個數組的交集
Array.intersect = function(a, b){
   return a.uniquelize().each(function(o){return b.contains(o) ? o : null});
};
 
//兩個數組的差集
Array.minus = function(a, b){
   return a.uniquelize().each(function(o){return b.contains(o) ? null : o});
};
 
//兩個數組的補集
Array.complement = function(a, b){
   return Array.minus(Array.union(a, b),Array.intersect(a, b));
};
 
//兩個數組並集
Array.union = function(a, b){
   return a.concat(b).uniquelize();
};

(2)使用樣例

var a = [1,2,3,4,5]
var b = [2,4,6,8,10]
console.log("數組a:", a);
console.log("數組b:", b);
console.log("a與b的交集:", Array.intersect(a, b));
console.log("a與b的差集:", Array.minus(a, b));
console.log("a與b的補集:", Array.complement(a, b));
console.log("a與b的並集:", Array.union(a, b));

方法二:使用 ES6 語法實現

1,實現原理
而在 ES6 中我們可以藉助擴展運算符(...)以及 Set 的特性實現相關計算,代碼也會更加簡單些。

2,樣例代碼

var a = [1,2,3,4,5]
var b = [2,4,6,8,10]
console.log("數組a:", a);
console.log("數組b:", b);
 
var sa = new Set(a);
var sb = new Set(b);
 
// 交集
let intersect = a.filter(x => sb.has(x));
 
// 差集
let minus = a.filter(x => !sb.has(x));
 
// 補集
let complement  = [...a.filter(x => !sb.has(x)), ...b.filter(x => !sa.has(x))];
 
// 並集
let unionSet = Array.from(new Set([...a, ...b]));
 
console.log("a與b的交集:", intersect);
console.log("a與b的差集:", minus);
console.log("a與b的補集:", complement);
console.log("a與b的並集:", unionSet);

方法三:使用 jQuery 實現

如果項目中有引入 jQuery,那麼實現起來也很簡單。


var a = [1,2,3,4,5]
var b = [2,4,6,8,10]
console.log("數組a:", a);
console.log("數組b:", b);
 
// 交集
let intersect = $(a).filter(b).toArray();
 
// 差集
let minus = $(a).not(b).toArray();
 
// 補集
let complement  = $(a).not(b).toArray().concat($(b).not(a).toArray());
 
// 並集
let unionSet = $.unique(a.concat(b));
 
console.log("a與b的交集:", intersect);
console.log("a與b的差集:", minus);
console.log("a與b的補集:", complement);
console.log("a與b的並集:", unionSet);

~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章