JS array operations: deduplication, intersection, union, difference set

duplicate removal

new map + filter

new map + filter

// Define the constant res, whose value is an instance of a Map object
// Returns the filtered result of the ARR array, which is an array
// The filter condition is that if there is no key in res, the value of that key is set to
const res = new Map(a);const arr = [1.1.2.2.3.4.4.5.6.6.6.6.4.3];
const result = arr.filter((a) = >! res.has(a) && res.set(a,1));
console.log(result); / / [6]
Copy the code

Array from + new Set

Array from + new Set


// A Set object is used to duplicate the array. A Set object is returned
// Convert a Set object to an array using the from method

const arr = [1.1.2.2.3.4.4.5.6.6.6.6.4.3];
const result = Array.from(new Set(arr));
console.log(result) / / [6].
Copy the code

ES6 extension (…). + new Set

ES6 extension (…). + new Set

// A Set object is used to duplicate the array. A Set object is returned
// Copy all traversable properties of the parameter object into the new array using the ES6 extension method

const arr = [1.1.2.2.3.4.4.5.6.6.6.6.4.3];
const result = [...new Set(arr)];
console.log(result) / / [6].
Copy the code

And set

concat + filter + includes

concat + filter + includes

let a = [1.2.3];
let b = [2.4.5];
let union = a.concat(b.filter(v= >! a.includes(v)));console.log(union) / / [1, 2, 3, 4, 5]
Copy the code

Array.from + new Set + concat

Array.from + new Set + concat


// A new array. from method in ES6 that converts array-like objects and traversable objects into arrays,
// Any class array with length can be converted to an array. Combined with Set structure to solve the mathematical Set
let a = [1.2.3];
let b = [2.4.5];
let aSet = new Set(a);
let bSet = new Set(b)
/ / and set
let union = Array.from(new Set(a.concat(b)));
console.log(union); / / [1, 2, 3, 4, 5]
Copy the code

concat + filter + indexOf

concat + filter + indexOf

Nans are not considered (arrays do not contain nans)


Nans are not included in the array.
let a = [1.2.3];
let b = [2.4.5];
let union = a.concat(b.filter(v= > a.indexOf(v) === -1));
console.log(union); / / [1, 2, 3, 4, 5]
Copy the code

Consider nans (NaN in array)


let a = [1.2.3.NaN];
let b = [2.4.5.NaN];
let aHasNaN = a.some(v= > isNaN(v));
let bHasNaN = b.some(v= > isNaN(v));
/ / and set
let union = a.concat(b.filter(v= > a.indexOf(v) === -1&&!isNaN(v))) .concat(! aHasNaN & bHasNaN ? [NaN] : []);

console.log(union); // [1, 2, 3, NaN, 4, 5]
Copy the code

intersection

filter + includes

filter + includes


let a = [1.2.3];
let b = [2.4.5];
let intersection = a.filter(v= > b.includes(v));
console.log(intersection); / / [2]
Copy the code

Array.from + new Set + filter

Array.from + new Set + filter


let a = [1.2.3];
let b = [2.4.5];
let aSet = new Set(a);
let bSet = new Set(b);
/ / intersection
let intersection = Array.from(new Set(a.filter(v= > bSet.has(v))));
console.log(intersection); / / [2]
Copy the code

filter + indexOf

filter + indexOf

Nans are not considered (arrays do not contain nans)


Nans are not included in the array.
let a = [1.2.3];
let b = [2.4.5];
let intersection = a.filter(v= > b.indexOf(v) > -1);
console.log(intersection); / / [2]
Copy the code

Consider nans (NaN in array)


// Consider NaN (array contains NaN)
let a = [1.2.3.NaN];
let b = [2.4.5.NaN];
let aHasNaN = a.some(v= > isNaN(v));
let bHasNaN = b.some(v= > isNaN(v));
let intersection = a.filter(v= > b.indexOf(v) > -1)
                    .concat(aHasNaN & bHasNaN ? [NaN] : []);

console.log(intersection); // [2, NaN]
Copy the code

Difference set

concat + filter + includes

concat + filter + includes


let a = [1.2.3];
let b = [2.4.5];
let difference = a.concat(b)
                  .filter(v= >! a.includes(v) || ! b.includes(v));console.log(difference)/ / 5-tetrafluorobenzoic [1]
Copy the code

Array.from + new Set + concat + filter

Array.from + new Set + concat + filter

let a = [1.2.3];
let b = [2.4.5];
let aSet = new Set(a);
let bSet = new Set(b)
/ / difference set
let difference = Array.from(new Set(a.concat(b).filter(v= >! aSet.has(v) || ! bSet.has(v))));console.log(difference) / / 5-tetrafluorobenzoic [1]
Copy the code

filter + indexOf + concat

filter + indexOf + concat

Nans are not considered (arrays do not contain nans)


Nans are not included in the array.
let a = [1.2.3];
let b = [2.4.5];
let difference = a.filter(v= > b.indexOf(v) === -1)
                  .concat(b.filter(v= > a.indexOf(v) === -1));

console.log(difference) / / 5-tetrafluorobenzoic [1]
Copy the code

Consider nans (NaN in array)


// Consider NaN (array contains NaN)
let a = [1.2.3.NaN];
let b = [2.4.5];
let aHasNaN = a.some(v= > isNaN(v));
let bHasNaN = b.some(v= > isNaN(v));
/ / difference set
let difference = a.filter(v= > b.indexOf(v) === -1&&!isNaN(v))
                  .concat(b.filter(v= > a.indexOf(v) === -1&&!isNaN(v))).concat(aHasNaN ^ bHasNaN ? [NaN] : []);

console.log(difference) // [1, 3, 4, 5, NaN]
Copy the code