1. 程式人生 > 實用技巧 >JS求陣列的交集、差集、並集、補集

JS求陣列的交集、差集、並集、補集

一、ES5

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

 1 var a = [1,2,3,4,5]
 2 var b = [2,4,6,8,10]
 3 //交集
 4 var c = a.filter(function(v){ return b.indexOf(v) > -1 })
 5 //差集
 6 var d = a.filter(function(v){ return b.indexOf(v) == -1 })
 7 //補集
 8 var e = a.filter(function(v){ return !(b.indexOf(v) > -1) })
 9  .concat(b.filter(function
(v){ return !(a.indexOf(v) > -1)})) 10 //並集 11 var f = a.concat(b.filter(function(v){ return !(a.indexOf(v) > -1)})); 12 console.log("陣列a:", a); 13 console.log("陣列b:", b); 14 console.log("a與b的交集:", c); 15 console.log("a與b的差集:", d); 16 console.log("a與b的補集:", e); 17 console.log("a與b的並集:", f);

2,對 Array 進行擴充套件

 1 //陣列功能擴充套件
 2 //陣列迭代函式
 3 Array.prototype.each = function(fn){
 4  fn = fn || Function.K;
 5  var a = [];
 6  var args = Array.prototype.slice.call(arguments, 1);
 7  for(var i = 0; i < this.length; i++){
 8  var res = fn.apply(this,[this[i],i].concat(args));
 9  if(res != null) a.push(res);
10  }
11 return a; 12 }; 13 //陣列是否包含指定元素 14 Array.prototype.contains = function(suArr){ 15 for(var i = 0; i < this.length; i ++){ 16 if(this[i] == suArr){ 17 return true; 18 } 19 } 20 return false; 21 } 22 //不重複元素構成的陣列 23 Array.prototype.uniquelize = function(){ 24 var ra = new Array(); 25 for(var i = 0; i < this.length; i ++){ 26 if(!ra.contains(this[i])){ 27 ra.push(this[i]); 28 } 29 } 30 return ra; 31 }; 32 //兩個陣列的交集 33 Array.intersect = function(a, b){ 34 return a.uniquelize().each(function(o){return b.contains(o) ? o : null}); 35 }; 36 //兩個陣列的差集 37 Array.minus = function(a, b){ 38 return a.uniquelize().each(function(o){return b.contains(o) ? null : o}); 39 }; 40 //兩個陣列的補集 41 Array.complement = function(a, b){ 42 return Array.minus(Array.union(a, b),Array.intersect(a, b)); 43 }; 44 //兩個陣列並集 45 Array.union = function(a, b){ 46 return a.concat(b).uniquelize(); 47 }; 48 //使用 49 var a = [1,2,3,4,5] 50 var b = [2,4,6,8,10] 51 console.log("陣列a:", a); 52 console.log("陣列b:", b); 53 console.log("a與b的交集:", Array.intersect(a, b)); 54 console.log("a與b的差集:", Array.minus(a, b)); 55 console.log("a與b的補集:", Array.complement(a, b)); 56 console.log("a與b的並集:", Array.union(a, b));

二、ES6

 1 var a = [1,2,3,4,5]
 2 var b = [2,4,6,8,10]
 3 console.log("陣列a:", a);
 4 console.log("陣列b:", b);
 5 var sa = new Set(a);
 6 var sb = new Set(b);
 7 // 交集
 8 let intersect = a.filter(x => sb.has(x));
 9 // 差集
10 let minus = a.filter(x => !sb.has(x));
11 // 補集
12 let complement = [...a.filter(x => !sb.has(x)), ...b.filter(x => !sa.has(x))];
13 // 並集
14 let unionSet = Array.from(new Set([...a, ...b]));
15 console.log("a與b的交集:", intersect);
16 console.log("a與b的差集:", minus);
17 console.log("a與b的補集:", complement);
18 console.log("a與b的並集:", unionSet);

三、Jquery

 1 var a = [1,2,3,4,5]
 2 var b = [2,4,6,8,10]
 3 console.log("陣列a:", a);
 4 console.log("陣列b:", b);
 5 // 交集
 6 let intersect = $(a).filter(b).toArray();
 7 // 差集
 8 let minus = $(a).not(b).toArray();
 9 // 補集
10 let complement = $(a).not(b).toArray().concat($(b).not(a).toArray());
11 // 並集
12 let unionSet = $.unique(a.concat(b));
13 console.log("a與b的交集:", intersect);
14 console.log("a與b的差集:", minus);
15 console.log("a與b的補集:", complement);
16 console.log("a與b的並集:", unionSet);