An array of
Var a = hc-positie [1]; Var b =,6,7,8 [2]; / / intersection a.f ilter (item = > b.i ncludes (item)); [...new Set(a.cat (b))] // complement a.feather (item=>! b.includes(item));Copy the code
Most of the group
Intersect (... A) {// intersection new Set deduplicatesif (a.length == 1) return a;
if (a.length == 2) {
return [...new Set(a[0].filter(x => a[1].includes(x)))];
}
for (let i = 0; i < a.length - 2; i++) {
//let set1, set2, set3;
let newSet;
// set1 = new Set(a[i]);
// set2 = new Set(a[i + 1]);
// set3 = new Set(a[i + 2]);
newSet = a[i].filter(x => a[i + 1].includes(x));
return[...new Set([...newSet].filter(x => a[i + 2].includes(x)))]; }},Copy the code
An array of objects
var a=[
{id:'001',name:'product01'},
{id:'002',name:'product02'},
{id:'003',name:'product03'},
{id:'004',name:'product04'},
{id:'005',name:'product05'}]; var b=[ {id:'003',name:'product03'},
{id:'006',name:'product06'},
{id:'007',name:'product07'},
{id:'008',name:'product08'},]; var obj={}; var arr=a.concat(b); // Intersection: define an object and determine the intersection by whether its attribute values occur more than once.function(pre,cur){ obj.hasOwnProperty(cur.id)? pre.push(cur):obj[cur.id]=true;
returnpre; } []); // merge: each traverse collects items that have not yet appeared arr.reduce(function(pre,cur){
if(! obj.hasOwnProperty(cur.id)){ pre.push(cur); } obj[cur.id]=true;
returnpre; },[]) //A is a subset of S, consisting of all elements in S that do not belong to A, called the absolute complement of subset A in S. // Shopping cart A group of goods, selected and unselected divided into two groups, separate for uselet test=a.reduce(function(pre,cur){
if(b.every(item=>item.id! ==cur.id)){ pre.push({ name:cur.name }) }returnpre; }, [])Copy the code