1. Directly use filter and concat to calculate

Var a = [1,2,3,4,5] var b = [2,4,6,8,10]Copy the code

/ / intersection

var c = a.filter(function(v){ return b.indexOf(v) > -1 })
Copy the code

/ / difference set

var d = a.filter(function(v){ return b.indexOf(v) == -1 })
Copy the code

/ / complement

var e = a.filter(function(v){ return! (b.indexOf(v) > -1) }) .concat(b.filter(function(v){ return! (a.indexOf(v) > -1)}))Copy the code
Var f = a.concat(b.filter(function(v){ return! (a.indexOf(v) > -1)})); console.log("Array A:", a);
console.log("Array B:", b);
console.log("Intersection of A and B:", c);
console.log("Difference set of A and B:", d);
console.log("Complement of A and B:", e);
console.log("Union of A and B:", f); Var = [1, 2, 3, 4, 5] var b =,4,6,8,10 [2] the console. The log ("Array A:", a);
console.log("Array B:", b); Var sa = new Set(a); var sb = new Set(b); / / intersectionletintersect = a.filter(x => sb.has(x)); / / difference setletminus = a.filter(x => ! sb.has(x)); / / complementletcomplement = [...a.filter(x => !sb.has(x)), ...b.filter(x => !sa.has(x))]; / / and setlet unionSet = Array.from(new Set([...a, ...b]));
 
console.log("Intersection of A and B:", intersect);
console.log("Difference set of A and B:", minus);
console.log("Complement of A and B:", complement);
console.log("Union of A and B:", unionSet); 3. 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); }returna; }; // Whether the Array contains the specified element array.prototype. contains =function(suArr){
  for(var i = 0; i < this.length; i ++){
      if(this[i] == suArr){
          return true; }}return false; } / / no duplicate elements Array. An Array of prototype. Uniquelize =function(){
   var ra = new Array();
   for(var i = 0; i < this.length; i ++){
      if(!ra.contains(this[i])){
          ra.push(this[i]);
      }
   }
   returnra; }; // Intersection of two arrays array.intersect =function(a, b){
   return a.uniquelize().each(function(o){returnb.contains(o) ? o : null}); }; // Array.minus =function(a, b){
   return a.uniquelize().each(function(o){returnb.contains(o) ? null : o}); }; Array.plement = array.plement =function(a, b){
   returnArray.minus(Array.union(a, b),Array.intersect(a, b)); }; // Union of two arrays array. union =function(a, b){
   return a.concat(b).uniquelize();
};
Copy the code

4. Array objects are similar

let students = [
		{
			name: "xxx",
			grade: 80,
			show: true
		},
		{
			name: "yyy",
			grade: 50,
			show: false}]let students4 = [
		{
			name: "xxx",
			grade: 80,
			show: true
		},
		{
			name: "zzz",
			grade: 80,
			show: true
		},
		{
			name: "yyy",
			grade: 50,
			show: false}]let arr8 = [...students].filter(x => [...students4].some(y => y.name === x.name));
	console.log(arr8)
Copy the code