ES6 Set sum of two arrays union, intersection, difference sets; array and to weight

 

 

Union:

arr1 const = [1, 2, 3, 2, 5 ];
arr2 const = [1, 4, 6, 8, 3 ];

// The two arrays into 
const concatArr = [... of arr1, ... arr2 is];

// array to re 
const the SET = new new the Set (concatArr);

const newArr = [...set]

 

Intersection:

arr1 const = [1, 2, 3, 2, 5 ];
arr2 const = [1, 4, 6, 8, 3 ];

const set1 = new Set(arr1);
const set2 = new Set(arr2);

const newArr = [...set1].filter(item => {
    return set2.has(item);
});

 

 

Difference set:

arr1 const = [1, 2, 3, 2, 5 ];
arr2 const = [1, 4, 6, 8, 3 ];

// First, to use the array to set ES6 weight: 
const = setl new new the Set (of arr1);
const set2 = new Set(arr2);

// then present were removed set1, set2 set2 in the absence and the presence, in the absence of a value set1 
const newArr1 = [... set1] .filter ((Item) => {
     return ! Set2.has (Item) ;
});
const newArr2 = [...set2].filter(item => {
    return !set1.has(item);
});

// two new array and then were combined, the difference can be set to give a 
const newArr = [... newArr1, ... newArr2];

 

Guess you like

Origin www.cnblogs.com/garfieldzhong/p/12075571.html